我正在尝试将颜色条添加到由两个具有相同宽高比的子图组成的图中,即set_aspect('equal'):

用于创建此图的代码可以在此IPython笔记本中找到.
使用下面显示的代码(以及笔记本中的代码)创建的图像是我可以获得的最佳结果,但它仍然不是我想要的.
plt.subplot(1,2,1)
plt.pcolormesh(rand1)
plt.gca().set_aspect('equal')
plt.subplot(1,2,2)
plt.pcolormesh(rand2)
plt.gca().set_aspect('equal')
plt.tight_layout()
from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(cax=cax)
Run Code Online (Sandbox Code Playgroud)

这个问题似乎有关:
我仍然不确定你到底想要什么,但我猜你想要pcolormesh在添加色条时使用相同尺寸的子图?
我现在所拥有的是一个黑客,因为我colorbar为两个子图添加了一个以确保它们具有相同的大小.后来我删除了第一个colorbar.如果结果是你想要的,我可以看一个更加pythonic的方式来实现它.就目前而言,你究竟想要什么仍然有点模糊.
import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = numpy.random.random((10, 10))
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1, aspect = "equal")
ax2 = fig.add_subplot(1,2,2, aspect = "equal")
im1 = ax1.pcolormesh(data)
im2 = ax2.pcolormesh(data)
divider1 = make_axes_locatable(ax1)
cax1 = divider1.append_axes("right", size="5%", pad=0.05)
divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("right", size="5%", pad=0.05)
#Create and remove the colorbar for the first subplot
cbar1 = fig.colorbar(im1, cax = cax1)
fig.delaxes(fig.axes[2])
#Create second colorbar
cbar2 = fig.colorbar(im2, cax = cax2)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

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