我想在 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()
Run Code Online (Sandbox Code Playgroud)
我曾尝试使用axis('off'),但这也会删除标签。(同样,如果我将它向上移动一行,即在ax.set_ylabel('second row').
所以到目前为止的结果是这样的:
我希望空的白框(不仅仅是它的黑色边界框或刻度和刻度标签)消失。这是可能的,如果是,我该如何实现?
不幸的是,您需要单独删除轴的元素以保留 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2806 次 |
| 最近记录: |