英文:
Move and align subplots to match a specific layout
问题
我有一个nxn矩阵,我想绘制它,还想绘制行和列的总和。
所以我有这个:
data = np.random.randn(5, 5)
fig, axes = plt.subplots(2, 2)
axes[0, 0].imshow(data)
axes[0, 1].imshow(data.sum(axis=1).reshape(-1, 1))
axes[1, 0].imshow(data.sum(axis=0).reshape(1, -1))
如何将行和列与主图像对齐并使它们靠近主图像?
我还想去掉右下角的空轴。
英文:
I have an nxn matrix that I want to plot, and I also want to plot the sum of rows and cols.
So I have this:
data = np.random.randn(5, 5)
fig, axes = plt.subplots(2, 2)
axes[0, 0].imshow(data)
axes[0, 1].imshow(data.sum(axis=1).reshape(-1, 1))
axes[1, 0].imshow(data.sum(axis=0).reshape(1, -1))
How can I align the row and column to the main image and put them closer to it?
I would also like to get rid of the empty axis in the bottom right.
答案1
得分: 1
以下是代码的翻译部分:
你可以尝试:
```python
data = np.random.randn(5, 5)
fig, axes = plt.subplots(2, 2,
gridspec_kw={'height_ratios': [5, 1],
'width_ratios': [5, 1]
}
)
axes[0, 0].imshow(data)
axes[0, 1].imshow(data.sum(axis=1).reshape(-1, 1))
axes[1, 0].imshow(data.sum(axis=0).reshape(1, -1))
axes[1, 1].set_visible(False)
plt.tight_layout()
输出:
<details>
<summary>英文:</summary>
You can try:
data = np.random.randn(5, 5)
fig, axes = plt.subplots(2, 2,
gridspec_kw={'height_ratios': [5, 1],
'width_ratios': [5, 1]
}
)
axes[0, 0].imshow(data)
axes[0, 1].imshow(data.sum(axis=1).reshape(-1, 1))
axes[1, 0].imshow(data.sum(axis=0).reshape(1, -1))
axes[1, 1].set_visible(False)
plt.tight_layout()
Output:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/ywIUr.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论