首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python的Matplotlib中,如何让瘦图像为主图像添加边框?

在Python的Matplotlib中,如何让瘦图像为主图像添加边框?
EN

Stack Overflow用户
提问于 2018-05-31 23:17:52
回答 1查看 181关注 0票数 1

我有一个2D矩阵Main,一个高度与Main匹配的Left矩阵,以及一个宽度与Main匹配的Top矩阵。我希望所有3个都有颜色条(由他们的个人限制),并为他们被缝合成一个图像,Top到顶部,LeftMain的左边。

最后,我希望能够在其他图的子图中绘制整个区块。

到目前为止,我一直在尝试axes_grid1,但我无法让薄的矩阵(和颜色条)粘在它们合适的边。我应该使用完全不同的东西吗?

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 06:20:11

我认为你几乎是正确的,但是没有必要创建多个分隔符来附加颜色条轴。我们可以使用原始的分隔符本身。我不得不使用append_axessize参数来使用下面的代码来获得正确的间距。关于将整个区块添加到另一个地块的子地块,这可能会带来更多的挑战,我还没有检查。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50627658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档