gpt4 book ai didi

python - Matplotlib 标记子图

转载 作者:行者123 更新时间:2023-11-28 18:09:25 25 4
gpt4 key购买 nike

我想为我的地 block 添加标签,该地 block 由子地 block 组成。这是我想要的(我用 GIMP 添加了外部标签)

enter image description here

这是我实际得到的:

enter image description here

这是生成最后一个图的代码:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

plots = 16
subplots = 9

fig = plt.figure(figsize=(8, 8))

wh_plots = int(np.sqrt(plots))
wh_subplots = int(np.sqrt(subplots))

outer_grid = gridspec.GridSpec(wh_plots, wh_plots, wspace=0.1, hspace=0.1)
for p in range(plots):
inner_grid = gridspec.GridSpecFromSubplotSpec(wh_subplots, wh_subplots, subplot_spec=outer_grid[p], wspace=0.05, hspace=0.05)
for s in range(subplots):
ax = plt.Subplot(fig, inner_grid[s])
ax.imshow(np.random.rand(10,10), cmap="magma", interpolation="none")
ax.set_xticks([])
ax.set_yticks([])
fig.add_subplot(ax)

if (p+1) > 12 and s == 7:
ax.set_xlabel("sub_xlabel")
if (p) % 4 == 0 and s == 3:
ax.set_ylabel("sub_ylabel")

all_axes = fig.get_axes()
plt.show()

我的问题:

  • 如何获得第一个图中看到的“xlabel”和“ylabel”?
  • 有没有更好的方法来标记子图(sub_xlabel/sub_ylabel)与我所做的相比?

    if (p+1) > 12 and s == 7:
    ax.set_xlabel("sub_xlabel")
    if (p) % 4 == 0 and s == 3:
    ax.set_ylabel("sub_ylabel")

有效,但看起来不对。

最佳答案

您可以在 plt.show() 之前添加这些行:

fig.text(0.5, 0.04, 'xlabel', ha='center', fontsize=18)
fig.text(0.04, 0.5, 'ylabel', va='center', rotation='vertical', fontsize=18)

关于python - Matplotlib 标记子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51627885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com