几个彩色条而不是

2024-09-27 09:25:55 发布

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

在我的天体物理学实习项目中,我正在创建一个信号过噪声图,我对我的子图有一个小问题,因为为了得到每个热图一个色条,我通过热图得到了3个色条,如下所示:

Signal over Noise map

这是我的剧本(希望它能被理解):

# VERIFICATION DE LA CARTE S/N
# convolution_X and convolution_mask_X are arrays

fig10 = plt.subplot(1,3,1)
step1 = convolution_locale - convolution_mask_locale
fig_step1 = plt.imshow(step1, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

fig10 = plt.subplot(1,3,2)
step2 = convolution_grande - convolution_mask_grande
fig_step2 = plt.imshow(step2, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

fig10 = plt.subplot(1,3,3)
S_N_map = step1 - step2
fig_S_N_map = plt.imshow(S_N_map, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

fig10 = plt.savefig(outname10)

你需要更多关于阵列的信息吗?在

如果你有主意,谢谢你!在


Tags: figpltmaskimshownearestinterpolationconvolutionstep1
2条回答

出于某些原因,图10积累了多余的颜色条;这将产生您想要的结果(我认为):

# VERIFICATION DE LA CARTE S/N
# convolution_X and convolution_mask_X are arrays
import numpy as np
import random

convolution_locale = np.array([[random.random() for _ in range(100)] for _ in range(100)])
convolution_grande = np.array([[random.random() for _ in range(100)] for _ in range(100)])

plt.subplot(1,3,1)
step1 = convolution_locale   #- convolution_mask_locale # uncomment to use your data
fig_step1 = plt.imshow(step1, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")
plt.show()

plt.subplot(1,3,2)
step2 = convolution_grande  #- convolution_mask_grande # uncomment to use your data
fig_step2 = plt.imshow(step2, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

plt.subplot(1,3,3)
S_N_map = step1 - step2
fig_S_N_map = plt.imshow(S_N_map, interpolation='nearest')
plt.colorbar()
plt.xlabel("X (arcmin)")
plt.ylabel("Y (arcmin)")

plt.savefig(outname10)

请注意,我修改了绘制的数组的构造(在代码中指出)

enter image description here

你的代码变得混乱,因为你没有告诉它哪个颜色条属于哪个子批次。在

在这里,使用matplotlib面向对象的方法将非常有帮助。如果您创建图形和子图实例(例如使用plt.subplots()),那么您可以在每个实例上调用绘图(例如ax1.imshow)和标记(例如ax1.set_xlabel)方法,而不需要使用plt。然后,当您创建颜色条(fig10.colorbar())时,您可以使用axkwarg来告诉它在哪个子批创建colorbar。在

您还将可映射对象(例如fig_step1)提供给对fig10.colorbar的调用,以确保使用正确的数组来创建colorbar比例。在

import matplotlib.pyplot as plt
import numpy as np

# Some fake data
outname10 = 'test.png'
convolution_locale = np.random.rand(250,250)
convolution_mask_locale = np.random.rand(250,250)
convolution_grande = np.random.rand(250,250)
convolution_mask_grande = np.random.rand(250,250)

# use a nice colormap
plt.viridis()

# Create the figure and subplot instances
fig10, (ax1, ax2, ax3) = plt.subplots(1,3)

step1 = convolution_locale - convolution_mask_locale
fig_step1 = ax1.imshow(step1, interpolation='nearest')
fig10.colorbar(fig_step1,ax=ax1)
ax1.set_xlabel("X (arcmin)")
ax1.set_ylabel("Y (arcmin)")

step2 = convolution_grande - convolution_mask_grande
fig_step2 = ax2.imshow(step2, interpolation='nearest')
fig10.colorbar(fig_step2,ax=ax2)
ax2.set_xlabel("X (arcmin)")
ax2.set_ylabel("Y (arcmin)")

S_N_map = step1 - step2
fig_S_N_map = ax3.imshow(S_N_map, interpolation='nearest')
fig10.colorbar(fig_S_N_map,ax=ax3)
ax3.set_xlabel("X (arcmin)")
ax3.set_ylabel("Y (arcmin)")

# Create space for labels between subplots
fig10.set_tight_layout(True)

fig10.savefig(outname10)

enter image description here

相关问题 更多 >

    热门问题