gpt4 book ai didi

python - 删除(子)图,但在 matplotlib 中保留轴标签

转载 作者:太空宇宙 更新时间:2023-11-04 00:20:52 24 4
gpt4 key购买 nike

我想在 matplotlib 中创建一个子图,例如,2 行和 2 列,但我只有 3 个要绘制的东西,并且希望将 左下角 子图留空。但是,我仍然希望在那个位置有一个 y 轴标签,它应该指代整个第二行。

到目前为止,这是我的代码:

import matplotlib.pyplot as plt

x = [0, 1]
y = [2, 3]

ax = plt.subplot2grid((2, 2), (0, 0))
ax.plot(x, y)
ax.set_ylabel('first row')

ax = plt.subplot2grid((2, 2), (0, 1))
ax.plot(x, y)

ax = plt.subplot2grid((2, 2), (1, 0))
ax.set_ylabel('second row')
# ax.axis('off') <---- This would remove the label, too

ax = plt.subplot2grid((2, 2), (1, 1))
ax.plot(x, y)

plt.show()

我尝试过使用 axis('off'),但这样也删除了标签。 (同样,如果我将它向上移动一行,即在 ax.set_ylabel('second row') 上方。

所以到目前为止的结果是这样的:

lower left plot y u no go away

我希望空的白框(不仅仅是它的黑色边界框或刻度和刻度标签)消失。这可能吗?如果可以,我该如何实现?

最佳答案

不幸的是,您需要单独删除轴的元素以保留 ylabel,因为 ylabel 本身也是轴的一个元素。

import matplotlib.pyplot as plt

fig, axes = plt.subplots(2,2)
fig.set_facecolor("#ecfaff")
for i, ax in enumerate(axes.flatten()):
if i!=2:
ax.plot([3,4,6])
if not i%2:
ax.set_ylabel("My label")

# make xaxis invisibel
axes[1,0].xaxis.set_visible(False)
# make spines (the box) invisible
plt.setp(axes[1,0].spines.values(), visible=False)
# remove ticks and labels for the left axis
axes[1,0].tick_params(left=False, labelleft=False)
#remove background patch (only needed for non-white background)
axes[1,0].patch.set_visible(False)

plt.show()

enter image description here

关于python - 删除(子)图,但在 matplotlib 中保留轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155654/

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