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

رسم خريطة حرارية ثنائية الأبعاد باستخدام Matplotlib و Numpy

Amer Abdallah

السؤال

أريد رسم خريطة حرارية ثنائية الأبعاد باستخدام Matplotlib، بياناتي عبارة عن مصفوفة Numpy، ولكل عنصر منها قيمة بين 0 و 1. لذا لكل عنصرين (i ، j) مثلًا في هذه المصفوفة، أريد رسم مربع عند الإحداثي (i ، j) في خريطة الحرارة، بحيث يتناسب لونها مع قيمة العنصر في المصفوفة.

بفرض أن شكل المصفوفة كالتالي:

>>> a = np.random.random((6, 6))
>>> a
array([[0.34569628, 0.05496669, 0.67590928, 0.90239245, 0.66320443,
        0.4946887 ],
       [0.85511099, 0.85745252, 0.91338568, 0.14328682, 0.94672538,
        0.0854923 ],
       [0.44951428, 0.90554375, 0.70846343, 0.70827077, 0.39701985,
        0.27419469],
       [0.50886559, 0.02832956, 0.66582517, 0.84427592, 0.62013073,
        0.27639197],
       [0.53300279, 0.4610318 , 0.35132157, 0.69305277, 0.02898212,
        0.73815412],
       [0.53661332, 0.61894501, 0.84694389, 0.33373854, 0.2278372 ,
        0.61519172]])
>>>

كيف اقوم بهذا الأمر وهل يجب أن أقوم بتحويل المصفوفة لنوع آخر حتى تعمل مع Matplotlib؟

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

Recommended Posts

  • 1

أولاً ال 2D-Heatmap هي خريطة التمثيل اللوني ثنائية الأبعاد هي أداة تصور البيانات التي تساعد على تمثيل حجم الظاهرة في شكل ألوان. في بايثون ، يمكننا رسم خرائط حرارية ثنائية الأبعاد باستخدام حزمة Matplotlib. هناك طرق مختلفة لرسم خرائط حرارية ثنائية الأبعاد ، ويمكنك تحقيق ذلك بعدة طرق. من خلال التابع imshow حيث أن cmap هي اسم خريطة الألوان colormap المستخدمة لربط البيانات العددية بالألوان بحيث كل عدد يقابل لون.:

import matplotlib.pyplot as plt
import numpy as np
# تشكيل بيانات عشوائية
a = np.random.random((20, 20))
#interpolation='nearest' و  cmap='hot' نحدد للتابع الوسطاء 
plt.imshow(a, cmap='hot', interpolation='nearest')
plt.show()

index.png.bd817fdf9783754648f9703efa64359a.png
هنا بنفس الطريقة لكن غيرنا خريطة الألوان:

import numpy as np
import matplotlib.pyplot as plt
a = np.random.random(( 4 , 4 ))
"""
[[0.18622235 0.55756219 0.08281777 0.081955  ]
 [0.12048756 0.4210682  0.7152597  0.37106132]
 [0.29459933 0.49586024 0.09527473 0.00433101]
 [0.68683861 0.63551886 0.47319173 0.82563014]]
"""
plt.imshow( a , cmap = 'autumn' , interpolation = 'nearest' )
plt.title( "2-D Heat Map" )
plt.show()

index3.png.d8bcdddf823a055adebab1e01304f02d.png
لاحظ كيف أن القيم الأكبر لونها أفتح والقيم الأصغر لونها أغمق. أو من خلال seaborn.heatmap:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
a = np.random.rand(10, 12)
ax = sns.heatmap(a, linewidth=0.5)
plt.show()

aindex.png.ca02e6d40e0a9cb5d54101d852b23cb7.png

وأخيراً من خلال التابع pcolormesh من الموديول matplotlib.pyplot:

import matplotlib.pyplot as plt
import numpy as np
a = np.random.rand( 4 , 4 )
plt.pcolormesh( a , cmap = 'summer' )
plt.title( '2-D Heat Map' )
plt.show()
# أو 
import matplotlib.pyplot as plt
import numpy as np
a = np.random.rand( 4 , 4 )
plt.pcolormesh( a , cmap = 'hot' ) # غيرنا الألوان
plt.title( '2-D Heat Map' )
plt.show()

idndex.png.571fb5678bdac79fb50a21ad3d604123.png

تم التعديل في بواسطة Ali Haidar Ahmad
رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

يمكنك رسم خريطه حرارية او ما عرف باسم heatmap بكثير ممن الطرق، دعنا نرى بعض اشهر تلك الطرق:

  • يمكنك استخدام الكود التالي:
import matplotlib.pyplot as plt
import numpy as np

# generate 2 2d grids for the x & y bounds
y, x = np.meshgrid(np.linspace(-3, 3, 100), np.linspace(-3, 3, 100))

z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
# x and y are bounds, so z should be the value *inside* those bounds.
# Therefore, remove the last value from the z array.
z = z[:-1, :-1]
z_min, z_max = -np.abs(z).max(), np.abs(z).max()

fig, ax = plt.subplots()

c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolormesh')
# set the limits of the plot to the limits of the data
ax.axis([x.min(), x.max(), y.min(), y.max()])
fig.colorbar(c, ax=ax)

plt.show()

سيكون الخرج الخاص به كالتالي: وهي خريطه حرارية تمثل النقط الاغمق فيها اماكن النقاط الاكثر تكرارا:

Gb2af.png.bcab1872b6beab42f51940c15dbf17b2.png

  • هناك طريقة أخرى متقدمه تستخدم في اعطاء معلومات اكثر تحديدا ووضوحا، عن طريق اعطاء النسب كارقام وكذلك الوان وليست الوان فقط وبالتالي هي اكثر تحديدا ووضوحا، انظر الكود التالي:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
              "potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
           "Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]

harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
                    [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
                    [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
                    [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
                    [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
                    [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
                    [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])


fig, ax = plt.subplots()
im = ax.imshow(harvest)

# We want to show all ticks...
ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(vegetables)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers)
ax.set_yticklabels(vegetables)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(len(vegetables)):
    for j in range(len(farmers)):
        text = ax.text(j, i, harvest[i, j],
                       ha="center", va="center", color="w")

ax.set_title("Harvest of local farmers (in tons/year)")
fig.tight_layout()
plt.show()

وتكون الخرج كالتالي:

 

heat.PNG.1ad553eb6fce28842ca4cb3ac7cf1ab1.PNG

وبالطبع تستطيع تغيير الالوان والعناوين سواء الافقية او الرأسية، وتمثل الارقام الظاهرة داخل كل مربع عدد المرات التي ظهرت فيها القيم المشتركه بين العمود والصف المتقاطعين في ذلك المربع.

تم التعديل في بواسطة Ahmed Sharshar
رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...