gpt4 book ai didi

python - Matplotlib Gridspec "Cell"标签

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:09 24 4
gpt4 key购买 nike

使用 GridSpec ,我有一个规则的地 block 格。假设 3 x 3。所有绘图轴都关闭,因为我对绘图的形状感兴趣,而不是单个轴值。

我想做的是标记较大框的 x 轴和 y 轴。例如,在上面的 3 x 3 情况下,x 轴可以是 ['A', 'B', 'C'],y 轴可以是 [1,2,3]。

是否有可能获得此标签?如何访问网格规范轴?

GridSpec documentation 中的内容不多除非我遗漏了一个明显的方法名称。

代码示例。数据位于 pandas 数据框中 - 忽略使用嵌套循环的强力提取...

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

gs = gridspec.GridSpec(40, 19, wspace=0.0, hspace=0.0)

for j in nseasons:
t = tt[j]
nlats = t.columns.levels[0]
for idx, k in enumerate(nlats):
diurnal = t[k].iloc[0]
ax = plt.subplot(gs[j, idx])
ax.plot(y, diurnal.values, 'b-')
ax.set_xticks([])
ax.set_yticks([])
fig.add_subplot(ax)
sys.stdout.write("Processed plot {}/{}\r".format(cplots, nplots))
sys.stdout.flush()
cplots += 1

#Here the figures axis labels need to be set.

最佳答案

如评论中所述,您可以使用 xlabel 和 ylabel 执行此操作,只需在图的左侧和底部标记轴。下面的例子。

from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec

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

rows = 40
cols = 19
gs = gridspec.GridSpec(rows, cols, wspace=0.0, hspace=0.0)

for i in range(rows):
for j in range(cols):
ax = plt.subplot(gs[i, j])
ax.set_xticks([])
ax.set_yticks([])

# label y
if ax.is_first_col():
ax.set_ylabel(i, fontsize = 9)

# label x
if ax.is_last_row():
ax.set_xlabel(j, fontsize = 9)

plt.show()

enter image description here

关于python - Matplotlib Gridspec "Cell"标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23916160/

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