在函数d中设置颜色条

2024-09-28 23:16:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于matplotlib和colorbar的问题。我的项目里有很多热图。假设我的数组是两个数组除以的结果。你知道吗

例如(为了使问题形象化):

import numpy as np
import matplotlib as plt

array_1 = A/B    # A and B are 2 arrays, same shape etc ..
array_2 = C/D    # C and D are 2 arrays, same shape etc ..

##################################
# Representation with matplotlib #
##################################

fig_step1 = ax1.imshow(array_1, interpolation='nearest')
fig1.colorbar(fig_step1,ax=ax1)
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")
ax1.set_title("array number 1")

fig_step2 = ax2.imshow(array_2, interpolation='nearest')
fig1.colorbar(fig_step2,ax=ax2)
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")
ax2.set_title("array number 2")

==>;但是,即使除法适用于99%的数据,并且我从数组1和数组2中得到值1的平均值。有时我有几个点(1%),例如A/B之间的结果是1000000,并使我的色条错误。你知道吗

==>;如何使用色条在热图中只打印[0;1]之间的点?也就是说,要有一个动态的色条?你知道吗

我为我的英语很差道歉:/

谢谢你!你知道吗


Tags: andimportmatplotlibasfig数组arrayare
1条回答
网友
1楼 · 发布于 2024-09-28 23:16:13

我想有两种解决方案:要么设置vminvmax的参数imshow,要么屏蔽数组中的无效值。你知道吗

import numpy as np
import matplotlib.pylab as plt

A = np.random.random((20,20))
B = np.random.random((20,20))

array_1 = A/B    # A and B are 2 arrays, same shape etc ..

fig1 = plt.figure()
ax1  = plt.subplot(121)
fig_step1 = ax1.imshow(array_1, interpolation='nearest')
fig1.colorbar(fig_step1,ax=ax1)
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")
ax1.set_title("Without limits")

ax2  = plt.subplot(122)
fig_step2 = ax2.imshow(array_1, interpolation='nearest', vmin=0, vmax=1)
fig1.colorbar(fig_step2, ax=ax2)
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")
ax2.set_title("With limits")

结果是: enter image description here

或者,如果要屏蔽无效值(假设所有大于1的值都无效):

import numpy as np
import matplotlib.pylab as plt

A = np.random.random((20,20))
B = np.random.random((20,20))

array_1 = A/B    # A and B are 2 arrays, same shape etc ..

# Mask the array with the required limits
array_1 = np.ma.masked_where(array_1 > 1, array_1)

fig1 = plt.figure()
ax1  = plt.subplot(111)
fig_step1 = ax1.imshow(array_1, interpolation='nearest')
fig1.colorbar(fig_step1,ax=ax1)
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")
ax1.set_title("No limits, but masked")

结果是:

enter image description here

相关问题 更多 >