【问题标题】:Several colorbars instead one几个颜色条而不是一个
【发布时间】:2016-04-04 12:56:37
【问题描述】:

我在我的天体物理学实习项目中创建了一个基于噪声图的信号,但我的子图有点问题,因为不是为每个热图获得一个颜色条,而是像这样通过热图获得 3 个颜色条:

这是我的脚本(希望它可以理解):

# 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)

您需要更多关于数组的信息吗?

如果你有想法,谢谢!

【问题讨论】:

标签: python arrays numpy matplotlib


【解决方案1】:

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

使用 matplotlib 面向对象的方法在这里会有很大帮助。如果您创建图形和子图实例(例如使用plt.subplots()),那么您可以在每个实例上调用绘图(例如ax1.imshow)和标记(例如ax1.set_xlabel)方法,而无需使用@987654326 @ 一点也不。然后,当您创建颜色条 (fig10.colorbar()) 时,您可以使用 ax kwarg 告诉它要在哪个子图旁边创建颜色条。

您还将可映射对象(例如 fig_step1)提供给对 fig10.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)

【讨论】:

  • 加上一个漂亮的颜色图...您显示的代码也比我建议的要好。
  • 谢谢!我等不及 jet 不再是默认颜色图了 :)
  • 然而,它抛出了一个错误:AttributeError: 'module' object has no attribute 'viridis' - 这是一个额外的导入还是我需要更新 matplotlib?
  • 和一个警告:warnings.warn("tight_layout : falling back to Agg renderer")
  • @tom 谢谢你的回答!它工作得很好:D 我可以在我的其他子图上应用这种过程并得到一些干净的东西:)
【解决方案2】:

由于某些原因,fig10 正在累积多余的彩条;这将产生你想要的(我认为):

# 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)

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

【讨论】:

  • 问题是step1和step2是两个数组的区别,不只是一个数组^^
  • 可以,不过没关系,就是简单的数据操作。你没有提供数组,所以我做了一些来说明这一点。您可以将其与您自己的数组一起使用,并在绘图之前以您喜欢的方式操作它们。
  • J'ai remis les instructions de soustraction des arrays - il faudra les de-commenter pour utiliser vos donnees.
  • 是的,我更新了我的脚本并且他正在运行,我正在等待进程结束,看看我的问题是否得到解决;)
猜你喜欢
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
相关资源
最近更新 更多