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

كيفية إزالة الفجوات gaps بين ال subplots في Matplotlib في بايثون

Ali Ismael

السؤال

كيف يمكننا حذف الفراغات (الفجوات) بين ال subplots التي تظهر عند عرضها في الكود التالي:

import matplotlib.pyplot as plt
for i in range(4):
    axs = plt.subplot(2, 2, i+1)
    axs.grid(b = True, color ='red',
		linestyle ='-.', linewidth = 0.5,
		alpha = 0.6)
    axs.set_xticklabels([])
    axs.set_yticklabels([])
plt.show()

index.png.ec9e166ce15e29f03353f49c77efe8a9.png
 

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

Recommended Posts

  • 1

يمكنك استخدام subplots_adjust وضبط المساحة طولاً وعرضاً على 0 (أو بالشكل الذي تريده) وذلك من خلال الوسيطين wspace و hspace كما يلي:

import matplotlib.pyplot as plt
for i in range(4):
    axs = plt.subplot(2, 2, i+1)
    axs.grid(b = True, color ='red',
		linestyle ='-.', linewidth = 0.5,
		alpha = 0.6)
    axs.set_xticklabels([])
    axs.set_yticklabels([])
# بعد الانتهاء من الرسم فقط أضف التعليمة التالية قبل عرض أو حفظ الصورة
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

والنتيجة:
1.png.dd842d43b69fa1ef5bbb8c4094156f5d.png
لكن تجدر الملاحظة في أنه في حالة استخدامك ل set_aspect('equal') أثناء رسم ال subplots أي إذا كنت قد أضفت التعليمة التالية أثناء رسم ال subplots : 

axs.set_aspect('equal')

 فلن يفيدك الحل السابق في تجنب الفراغات الأفقية، لاحظ:

import matplotlib.pyplot as plt
for i in range(4):
    axs = plt.subplot(2, 2, i+1)
    axs.grid(b = True, color ='red',
		linestyle ='-.', linewidth = 0.5,
		alpha = 0.6)
    axs.set_xticklabels([])
    axs.set_yticklabels([])
    # هنا أضفنا التعليمة
    axs.set_aspect('equal')
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

ستكون النتيجة:
2.png.6a7ed8bbf7d9201d2ff32b359806e3fb.png
الاختلاف في هذه الحالة الثانية هو أنك أجبرت المحورين x و y على الحصول على نفس عدد البكسلات.  ونظراً لأن المحاور تنتقل من 0 إلى 1 بشكل افتراضي (أي قبل رسم أي شيء) ، فإن استخدام set_aspect('equal')  تؤدي إلى جعل المحورين x و y في كل subplot متساويين (أي مربع). ونظراً لأن ال figure ليس مربعاً ، فإن pyplot يضيف مسافات إضافية بين المحاور أفقياً. ولحل المشكلة يجب أن تقوم بتحديد أبعاد ال figure للحصول على نسبة العرض إلى الارتفاع الصحيحة:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4,4)) # subplot نحدد أبعاده بحيث يتطابق مع عدد ال 
for i in range(4):
    axs = plt.subplot(2, 2, i+1)
    axs.grid(b = True, color ='red',
		linestyle ='-.', linewidth = 0.5,
		alpha = 0.6)
    axs.set_xticklabels([])
    axs.set_yticklabels([])
    # هنا أضفنا التعليمة
    axs.set_aspect('equal')
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

والنتيجة:
3.png.c879064a4fb6895719b133c7bb0dbe82.png

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

  • 1

هناك طريقة مشهورة وهي  aspect='equal'لكن تعاني من مشكلة وهي انه الرسومات لا تتمدد بنفس النسبة لتغطي كل الفراغات.

وبالاضافة لكل الطرق التي ذكرها علي، يمكنك استخدام الكود التالي ببساطة:

import matplotlib.pyplot as plt

plt.clf()
f, axarr = plt.subplots(2, 2, gridspec_kw = {'wspace':0, 'hspace':0})

for i, ax in enumerate(f.axes):
    ax.grid('on', linestyle='--')
    ax.set_xticklabels([])
    ax.set_yticklabels([])

plt.show()
plt.close()

وتظهر هكذا:

45.png.3282e5579cf0e61e275c65a86ab39006.png

أو يمكنك استخدام gridspec  لضبط المسافة بين اللرسومات يدويا كالتالي:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

plt.figure(figsize = (2,2))
gs1 = gridspec.GridSpec(2, 2)
gs1.update(wspace=0.025, hspace=0.05) # ضبط المسافات 

for i in range(4):
    ax1 = plt.subplot(gs1[i])
    plt.axis('on')
    ax1.grid(b = True, color ='red',
		linestyle ='-.', linewidth = 0.5,
		alpha = 0.6)
    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_aspect('equal')

plt.show()

وتظهر هكذا:

333.png.41d0eaea3ce626ff892324a3f08cf519.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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...