Bog*_*dan 4 python matplotlib colorbar
我想知道是否有可能有一个子图,其中混合使用plot()绘制的图形和使用pcolormesh()绘制的图形具有相同的x轴长度.
当x轴相同长度的pcolormesh()子图没有绘制颜色条时,我附加了2张图像
当pcolormesh()子图缩小时,它具有colorbar,以便容纳colorbar. 
我想有颜色条,但它应该延长整个图的长度而不缩小子图.
如果没有对任何当前类进行子类化,这是否可行?
Joe*_*ton 10
当然,这是可能的!
发生的事情是正在为颜色条创建一个新轴,并且从pcolormesh绘制的轴中获取空间.如果您不希望发生这种情况,可以指定颜色条的轴对象进入或者,您可以使用水平颜色条.
无论如何,让我们用独立数据重现您的问题:
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()
Run Code Online (Sandbox Code Playgroud)

快速解决方法是只使用水平颜色条:
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()
Run Code Online (Sandbox Code Playgroud)

或者,您可以手动为颜色条添加另一个轴.我们还会调整一些东西以腾出更多空间.
(旁注: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()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
3307 次 |
| 最近记录: |