简体   繁体   English

如何在其他子图中绘制Contourf Colorbar-Matplotlib

[英]How to plot contourf colorbar in different subplot - matplotlib

This is a very similar question to "How to plot pcolor colorbar in a different subplot - matplotlib" . 这与“如何在另一个子图-matplotlib中绘制pcolor colorbar”非常类似。 I am trying to plot a filled contour plot and a line plot with a shared axis and the colorbar in a separate subplot (ie so it doesn't take up space for the contourf axis and thus muck up the x-axis sharing). 我正在尝试在单独的子图中绘制具有共享轴和颜色栏的填充轮廓图和折线图(即,这样就不会占用轮廓f轴的空间,因此会破坏x轴共享)。 However, the x-axis in my code does not rescale nicely: 但是,我的代码中的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()

which produces an x-axis scaled from 0 to 20 (inclusive) rather than 0 to 19, which means there is unsightly whitespace in the filled contour plot. 这会产生从0到20(包括0)(而不是从0到19)缩放的x轴,这意味着填充轮廓图中存在难看的空白。 Commenting out the sharex=ax1 in the above code means that the x-axis for the contour plot is scaled nicely, but not for the line plot above it and the plt.tick_params code has no effect on either axis. sharex=ax1上面代码中的sharex=ax1意味着轮廓图的x轴可以很好地缩放,但上方的线图则不能缩放,并且plt.tick_params代码plt.tick_params两个轴均无效。

情节

Is there a way of solving this? 有办法解决吗?

You could also turn off the autoscaling of x-axis for all subsequent call of plot on this axis so that it keeps the range set by contourf and sharex=True : 您还可以关闭此轴上所有后续绘图的x轴的自动缩放,以使其保持由contourfsharex=True设置的范围:

ax2.set_autoscalex_on(False)

This comes even before your call to ax2.plot() and I think it is better than calling ax2.set_xlim(0, 19) since you do not need to know what are the actual limit of your x-axis that may be needed. 这甚至在您调用ax2.plot()之前就ax2.plot()并且我认为它比调用ax2.set_xlim(0, 19) 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,
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.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()

例

You can use inset_axes for this without added another axis. 您可以为此使用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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM