我有一个2D矩阵Main
,一个高度与Main
匹配的Left
矩阵,以及一个宽度与Main
匹配的Top
矩阵。我希望所有3个都有颜色条(由他们的个人限制),并为他们被缝合成一个图像,Top
到顶部,Left
到Main
的左边。
最后,我希望能够在其他图的子图中绘制整个区块。
到目前为止,我一直在尝试axes_grid1
,但我无法让薄的矩阵(和颜色条)粘在它们合适的边。我应该使用完全不同的东西吗?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Data
n = 20
Main = np.random.randn(n, n)
Left = np.random.randn(n, 1)
Top = np.random.randn(n, 2)
# Setup
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
ax_top = divider.append_axes("top", 1., pad=0.)
ax_left = divider.append_axes("left", 1., pad=0.)
ax_right = divider.append_axes("right", 1., pad=0.)
# Main
im_main = ax.imshow(Main)
clrbar_right = plt.colorbar(im_main, cax=ax_right)
# Top
im_top = ax_top.imshow(Top.T)
ax_top_divider = make_axes_locatable(ax_top)
ax_top_top = ax_top_divider.append_axes('top', 1., pad=0.)
ax_top_clrbar = plt.colorbar(im_top, cax=ax_top_top, orientation='horizontal')
# Left
im_left = ax_left.imshow(Left)
ax_left_divider = make_axes_locatable(ax_left)
ax_left_left = ax_left_divider.append_axes('left', 1., pad=0.)
ax_left_clrbar = plt.colorbar(im_left, cax=ax_left_left)
plt.show()
发布于 2018-06-01 06:20:11
我认为你几乎是正确的,但是没有必要创建多个分隔符来附加颜色条轴。我们可以使用原始的分隔符本身。我不得不使用append_axes
的size
参数来使用下面的代码来获得正确的间距。关于将整个区块添加到另一个地块的子地块,这可能会带来更多的挑战,我还没有检查。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Data
n = 20
Main = np.random.randn(n, n)
Left = np.random.randn(n, 1)
Top = np.random.randn(n, 2)
# Setup
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
# The top and left axes for images
ax_top = divider.append_axes('top',size='10%',pad=0.0)
ax_left = divider.append_axes('left',size='5%', pad=0.)
# The color bar axes
ax_top_top = divider.append_axes('top', size='5%',pad=0.1)
ax_left_left = divider.append_axes('left',size='5%', pad=0.1)
ax_right = divider.append_axes('right',size='5%', pad=0.1)
# Main
im_main = ax.imshow(Main)
clrbar_right = plt.colorbar(im_main, cax=ax_right)
# Top
im_top = ax_top.imshow(Top.T)
ax_top_clrbar = plt.colorbar(im_top, cax=ax_top_top, orientation='horizontal')
# Left
im_left = ax_left.imshow(Left)
ax_left_clrbar = plt.colorbar(im_left, cax=ax_left_left)
# Turn off the axis labels for the images if you like
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax_top.axes.get_xaxis().set_visible(False)
ax_top.axes.get_yaxis().set_visible(False)
ax_left.axes.get_xaxis().set_visible(False)
ax_left.axes.get_yaxis().set_visible(False)
# Switch the ticks for the color bars on the left and the top
ax_left_left.yaxis.tick_left()
ax_top_top.xaxis.tick_top()
plt.show()
https://stackoverflow.com/questions/50627658
复制相似问题