简体   繁体   English

当色条打开时,Matplotlib将绘图与pcolor(网格)混合在一起,使其具有相同的x轴长度

[英]Matplotlib mixed subplots with plot and pcolor(mesh) to have the same x axis length when colorbar is on

I am wondering if it is possible to have a subplot with a mixture of graphs drawn with plot() and figures drawn with pcolormesh() that would have the same x axis length. 我想知道是否有可能有一个子图,其中混合使用plot()绘制的图形和使用pcolormesh()绘制的图形具有相同的x轴长度。

I attached 2 images one when the pcolormesh() subplot does not have the colorbar drawn when the x axes are the same length 当x轴相同长度的pcolormesh()子图没有绘制颜色条时,我附加了2张图像 在此输入图像描述 and one that has the colorbar when the pcolormesh() subplot is shrunk so that it accommodates the colorbar. 当pcolormesh()子图缩小时,它具有colorbar,以便容纳colorbar。 在此输入图像描述

I would like to have the colorbar, but it should be extending the length of the whole plot and not shrink the subplot. 我想有颜色条,但它应该延长整个图的长度而不缩小子图。

Is this possible without sub-classing any of the current classes? 如果没有对任何当前类进行子类化,这是否可行?

Sure, it's possible! 当然,这是可能的!

What's happening is that a new axes is being created for the colorbar, and the space is being taken from the axes that pcolormesh is plotted in. If you don't want this to happen, you can specify an axes object for the colorbar to go in. Alternately, you could just use a horizontal colorbar. 发生的事情是为pcolormesh创建了一个新轴,并且正在从绘制了pcolormesh的轴中pcolormesh空间。如果您不希望发生这种情况,可以指定pcolormesh的轴对象去或者,您可以使用水平颜色条。

At any rate, let's reproduce your problem with stand-alone data: 无论如何,让我们用独立数据重现您的问题:

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1977)

# Generate some interesting-looking random data...
num = 200
grid = np.random.normal(0, 1, (20, num)).cumsum(axis=1).cumsum(axis=0)
x = np.linspace(0, 360, num)
y1 = np.random.normal(0, 1, num).cumsum()
y2 = np.random.normal(0, 1, num).cumsum()

# Plot on three seperate axes
fig, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
im = axes[2].imshow(grid, extent=[0, 360, 0, 20], aspect='auto')
fig.colorbar(im)

plt.show()

在此输入图像描述


A quick fix is to just use a horizontal colorbar: 快速解决方法是使用水平颜色条:

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1977)

# Generate some interesting-looking random data...
num = 200
grid = np.random.normal(0, 1, (20, num)).cumsum(axis=1).cumsum(axis=0)
x = np.linspace(0, 360, num)
y1 = np.random.normal(0, 1, num).cumsum()
y2 = np.random.normal(0, 1, num).cumsum()

# Plot on three seperate axes
fig, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
im = axes[2].imshow(grid, extent=[0, 360, 0, 20], aspect='auto')
fig.colorbar(im, orientation='horizontal')

plt.show()

在此输入图像描述


Alternately, you can manually add another axes for the colorbar. 或者,您可以手动为颜色条添加另一个轴。 We'll also adjust things a bit to make more room. 我们还会调整一些东西以腾出更多空间。

(Side note: Don't use tight_layout after making the extra axes. We're no longer working with a nice grid of subplots once we add the additional one, so tight_layout will not work correctly. It's safe to use it before, though you may want to modify the call to subplots_adjust in that case.) (旁注:制作额外的轴后不要使用tight_layout 。一旦我们添加额外的一个,我们就不再使用一个很好的子图网格了,所以tight_layout将无法正常工作。以前使用它是安全的,尽管你在这种情况下,可能想要修改对subplots_adjust的调用。)

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1977)

# Generate some interesting-looking random data...
num = 200
grid = np.random.normal(0, 1, (20, num)).cumsum(axis=1).cumsum(axis=0)
x = np.linspace(0, 360, num)
y1 = np.random.normal(0, 1, num).cumsum()
y2 = np.random.normal(0, 1, num).cumsum()

# Plot on three seperate axes
fig, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
im = axes[2].imshow(grid, extent=[0, 360, 0, 20], aspect='auto')

# Make some room for the colorbar
fig.subplots_adjust(left=0.07, right=0.87)

# Add the colorbar outside...
box = axes[2].get_position()
pad, width = 0.02, 0.02
cax = fig.add_axes([box.xmax + pad, box.ymin, width, box.height])
fig.colorbar(im, cax=cax)

plt.show()

在此输入图像描述

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

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