اذهب إلى المحتوى
  • 0

كيف يمكنني تغيير لون خلفية الرسم plot في Matplotlib | بايثون

Ali Ismael

السؤال

لدي plot وأريد جعل لون خلفية له سوداء، كيف يمكنني تغييرها؟ علماً أنني حاول استخدام set_facecolor لكن ظهر كما في الصورة فما السبب وما الحل؟

import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(1, 2, 5000)
y = np.random.normal(-1, 3, 2000)
bins = np.linspace(-10, 10, 30)
fig = plt.figure()
fig.patch.set_facecolor('black')
plt.hist([x,y], bins = 15,label=[x,y],color=["red","blue"])
legend = ['Data1','Data2']
plt.legend(legend)
plt.show()

index.png.aaed70e92acbedbd9e20d76d676f2cdf.png
 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

يمكنك استخدام set_facecolor(color) للتحكم بألوان أي axes لديك لكن يجب أن تكون دقيقاُ في استخدامها، فمثلاً لتغيير لون الخلفية في مثالك نقوم بأخذ كائن يمثل ال axes (المساحة التي تظهر فيها الصورة) الخاص بك ثم نستدعي منه الدالة كالتالي:

import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(1, 2, 5000)
y = np.random.normal(-1, 3, 2000)
bins = np.linspace(-10, 10, 30)
# الخاص بك axes أخذ غرض يمثل ال 
ax = plt.axes()
ax.set_facecolor('black')
# هنا فقط لإضافة خطوط شبكة للرسم أي يمكنك تجاهلها
ax.grid(b = True, color ='w',
		linestyle ='-.', linewidth = 0.5,
		alpha = 0.6)
plt.hist([x,y], bins = 15,label=[x,y],color=["red","blue"])
legend = ['Data1','Data2']
plt.legend(legend)
plt.show()

الخرج:
2.png.e292883d220acd04cbb059cced5ad4a9.png
أما ماتقوم به أنت هو تلوين ال figure الذي يحتوي ال axes، فكما نعلم أن ال figure يحتوي ال axes (واحد أو أكثر) انظر للصورة لتفهم ما أقصد:
4.png.49753479079dcab24d6cea2ec1c83c03.png
حيث قمت بأخذ كائن يمثل ال figure ثم قمت بضبط لونه، فظهر بذلك الشكل. وكحل آخر يمكنك استخدام rcParams كالتالي:

import matplotlib.pyplot as plt
plt.rcParams['axes.facecolor'] = 'black'
رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 1

يمكنك استخدام ax.set_facecolor والذي يمكنك بالتحكم بالوان الرسومات بشكل كبير ابتداء من تغيير الخلفية وحتى تغيير الوان بداخل بعضها كما سنري في الامثلة التالية.

الولا دعنا ننشئ رسم بشكل بسيط:

# استدعاء الدالة
import matplotlib.pyplot as plt
  
# اعطاء قيم
student_marks = [50, 60, 70, 80, 90]
student_grade = ['B', 'B', 'B+', 'B+', 'A']
  
plt.plot(student_marks, student_grade)
  
# اعطاء اسماء للمحاور
plt.xlabel("student_marks", fontweight='bold')
plt.ylabel("student_grade", fontweight='bold')
  
# اعطاء اسم للرسم
plt.title("Student Marks v/s Student Grade")
  
# عرض الرسم
plt.show()

تظهر كأي رسم اعتيادي كالتالي:

6168b929b3dbb_Capture(1).thumb.png.5cc3e3f26286ac8714b24f051c493614.png

ويمكننا تغيير لون الخلفية للون الأصفر بسهولة كالتالي:
 

#نفس السابق
import matplotlib.pyplot as plt

student_marks = [50, 60, 70, 80, 90]
student_grade = ['B', 'B', 'B+', 'B+', 'A']
plt.plot(student_marks, student_grade)

plt.xlabel("student_marks", fontweight='bold')
ax = plt.axes()
  
# تغيير لون الخلفية للاصفر
ax.set_facecolor("yellow")
  
plt.ylabel("student_grade", fontweight='bold')
  
plt.title("Student Marks v/s Student Grade")
  

plt.show()

وتظهر هكذا:

Figure1.png.c628c2309ea0786ed18c730884b51355.png

يمكنك كذلك تغيير اللون الداخلى والخارجي للرسم بسهولة:

# استدعاء المكتبات
import matplotlib.pyplot as plt
import numpy as np

# اعطاء قيم
x = np.arange(0, 10, .1)
y = np.sin(x)

# ضبط لون الخلفية الخارجي
plt.figure(facecolor='#94F008')

# الرسم
plt.plot(x, y)

# اعطاء اسماء للمحاور
plt.xlabel("X")
ax = plt.axes()

# ضبط لون الخلفية الداخلية
ax.set_facecolor("#1CC4AF")

plt.ylabel('sin(x)')

# اظهار الرسم
plt.show()

وتظهر هكذا:

Figure12.png.3b61522985f21c72af7ba5841bb46f1e.png

لاحظ امكانية كتابة اللون اما باسمه اذا كان متوافرا أو بالكود الخاص به.

 

 

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...