【问题标题】:How to plot contourf colorbar in different subplot - matplotlib如何在不同的子图中绘制轮廓颜色条 - matplotlib
【发布时间】:2015-09-08 10:19:26
【问题描述】:

这是一个与"How to plot pcolor colorbar in a different subplot - matplotlib" 非常相似的问题。我正在尝试在单独的子图中绘制一个填充的等高线图和一个带有共享轴和颜色条的线图(即,它不会占用 contourf 轴的空间,因此会破坏 x 轴共享)。但是,我的代码中的 x 轴不能很好地重新缩放:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

z = np.random.rand(20, 20)
x, y = np.arange(20), np.arange(20)
y2 = np.random.rand(20)
fig = plt.figure(figsize=(8, 8))
gs = mpl.gridspec.GridSpec(2, 2, height_ratios=[1, 2], width_ratios=[2, 1])
ax1 = fig.add_subplot(gs[1, 0])
ax2 = fig.add_subplot(gs[0, 0], sharex=ax1)
ax3 = fig.add_subplot(gs[1, 1])
cont = ax1.contourf(x, y, z, 20)
plt.tick_params(which='both', top=False, right=False)
ax2.plot(x, y2, color='g')
plt.tick_params(which='both', top=False, right=False)
cbar = plt.colorbar(cont, cax=ax3)
cbar.set_label('Intensity', rotation=270, labelpad=20)
plt.tight_layout()
plt.show()

这会产生一个从 0 到 20(含)而不是 0 到 19 的 x 轴,这意味着填充的等高线图中存在难看的空白。在上面的代码中注释掉 sharex=ax1 意味着等高线图的 x 轴缩放得很好,但对于上面的线图则没有,plt.tick_params 代码对任一轴都没有影响。

有没有办法解决这个问题?

【问题讨论】:

  • 我将情节作为图像包含在内。仅凭您的描述很难理解问题。如果您不喜欢这个地方,请随意编辑和(重新)移动它。
  • ax1.set_xlim(0, 19) 应该是一个快速的解决方法。
  • 谢谢,塞尔。奇怪的是,添加 ax1.set_xlim 不起作用。我应该在问题中提到这一点。 contourf 的行为似乎是出乎意料的(对我来说),它干扰或阻止了轴方法。
  • 按照@cel 的建议设置xlim 对我有用:i.imgur.com/LvR3UnF.png
  • 对不起,@cel 和 @tom,我是个白痴。使用set_xlim 工作正常。此外,使用 ax1.set_params() 有效,而 plt.set_params() 无效。不知道为什么。

标签: python matplotlib


【解决方案1】:

您还可以关闭 x 轴的自动缩放,以便在此轴上随后调用 plot 以保持 contourfsharex=True 设置的范围:

ax2.set_autoscalex_on(False)

这甚至在您调用 ax2.plot() 之前就已经出现了,我认为这比调用 ax2.set_xlim(0, 19) 更好,因为您不需要知道可能需要的 x 轴的实际限制是多少。


import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
 
z = np.random.rand(20, 20)
x, y = np.arange(20), np.arange(20)
y2 = np.random.rand(20)
fig = plt.figure(figsize=(8, 8))
gs = mpl.gridspec.GridSpec(2, 1, height_ratios=[1, 2], width_ratios=[2])
ax1 = fig.add_subplot(gs[1, 0])
ax2 = fig.add_subplot(gs[0, 0], sharex=ax1)
cont = ax1.contourf(x, y, z, 20)
plt.tick_params(which='both', top=False, right=False)

ax2.set_autoscalex_on(False)
ax2.plot(x, y2, color='g')

axins = inset_axes(ax1,
           width="5%", # width = 10% of parent_bbox width
           height="100%", # height : 50%
           loc=6,
           bbox_to_anchor=(1.05, 0., 1, 1),
           bbox_transform=ax1.transAxes,
           borderpad=0,
       )

cbar = plt.colorbar(cont, cax=axins)
plt.show()

【讨论】:

  • width_ratios=[2,1]) 应该是width_ratios=[2])
【解决方案2】:

您可以为此使用 inset_axes 而无需添加另一个轴。

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

z = np.random.rand(20, 20)
x, y = np.arange(20), np.arange(20)
y2 = np.random.rand(20)
fig = plt.figure(figsize=(8, 8))
gs = mpl.gridspec.GridSpec(2, 2, height_ratios=[1, 2], width_ratios=[2, 1])
ax1 = fig.add_subplot(gs[1, 0])
ax2 = fig.add_subplot(gs[0, 0], sharex=ax1)
cont = ax1.contourf(x, y, z, 20)
plt.tick_params(which='both', top=False, right=False)
ax2.plot(x, y2, color='g')
plt.tick_params(which='both', top=False, right=False)




axins = inset_axes(ax1,
               width="5%", # width = 10% of parent_bbox width
               height="100%", # height : 50%
               loc=6,
               bbox_to_anchor=(1.05, 0., 1, 1),
               bbox_transform=ax1.transAxes,
               borderpad=0,
           )

cbar = plt.colorbar(cont, cax=axins)
plt.savefig('figure.jpg',bbox_inches='tight',dpi=200)

【讨论】:

  • 这没有回答问题,即如何删除contourf 绘图上x=19x=20 之间的空白
  • 哇!我不敢相信我错过了。当我阅读这个问题时,我将其读作询问如何插入颜色条而不干扰第二个绘图相对于第一个绘图的缩放。谈论错过显而易见的事情......哦,真可惜。
  • 虽然我在inset_axes 学到了一些新东西,所以我很感激这个答案。为什么plt.tick_params()适用于上轴而不适用于上面的下轴?
  • @tnknepp 发生在我们当中!
  • t.wood:在您发布的代码中,关于轴顺序,事情变得有点混乱(至少对我而言)。 ax2 是上图,而 ax1 是下图(通常我会称上图为 ax1,下图为 ax2……但这只是偏好)。关键是,ax2 仍然是最后创建的轴,因此您的 tick_params() 命令会发给它。您可以通过直接向轴显式发出命令来解决此问题,这就是我在代码中总是这样做的方式。例如ax1.tick_params()
猜你喜欢
  • 2016-03-14
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多