python - 在 Matplotlib 中为文本添加没有轴刻度/标签的空子图作为子图
问题描述
我的目标是创建带有四个子图的图,其中底部的两个实际上只是空框,我将在其中显示一些文本。不幸的是,我删除 y 和 x 轴刻度线和标签的所有努力都失败了。我还是 matplotlib 的新手,所以我确信我缺少一些简单的东西。这是我正在尝试和得到的结果:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, sharex=False, sharey=True, figsize=(6,6))
fig.add_subplot(111, frameon=False)
plt.tick_params(labelcolor='none', top=False, bottom=False, left=False, right=False)
plt.title('Neuron Length')
plt.xlabel('Strain')
plt.ylabel('Neuron Length (um)')
aIP = fig.add_subplot(223, frameon=False)
aIP.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
xycoords='axes fraction', va='center')
# First approach
aIP.axes.xaxis.set_ticks([])
aIP.axes.yaxis.set_ticks([])
# Second approach
ax = plt.gca()
ax.axes.yaxis.set_visible(False)
plt.show()
解决方案
这是通过使用plt.subplots()
绘制其中四个并删除左下角的框架来实现的。
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(-np.pi, np.pi, 1000)
x1 = np.sin(2*t)
x2 = np.cos(2*t)
x3 = x1 + x2
fig,axes = plt.subplots(nrows=2,ncols=2,figsize=(6,6), sharex=True, sharey=True)
axes[0,0].plot(t, x1, linewidth=2)
axes[0,1].plot(t, x2, linewidth=2)
axes[1,1].plot(t, x3, linewidth=2)
axes[1,0].axis('off') # off
axes[1,0].annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5), xycoords='axes fraction', va='center')
fig.suptitle('Neuron Length')
for ax in axes.flat:
ax.set(xlabel='Strain', ylabel='Neuron Length (um)')
plt.show()
推荐阅读
- python - 如果没有 OCR,如何检测两个文本图像是否相似?
- python - 当我尝试获取 Ui_MainWindow 类中的值时,df 为空
- laravel - 如何添加警报单击按钮提交数据表 yajra ?拉拉维尔
- java - 空行的Java扫描仪测试
- charts - 如何避免 Google 折线图注释中的重叠
- android - 我想使用适配器和模型类将我的嵌套 JSON 数据显示到 recylerview
- django - /kitty_view 上的 NoReverseMatch 未找到带有参数“(5,)”的“kitty”的反向。我为另一个应用程序尝试过的相同网址,并且正在运行
- asp.net - 我们如何在linkedin auth api v2中获取个人资料网址?
- javascript - javascript日期未正确返回
- reactjs - react-redux中connect()中用到的参数需要说明一下