pandas - Pandas 条形图和子图中的标签
问题描述
df=pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
x=[1,2,3,4,5]
y=[1,4,9,16,25]
fig, axes = plt.subplots(figsize=(8,8),nrows=2, ncols=2)
ax1=plt.subplot(2,2,1)
plt.plot(x,y)
#ax2=plt.subplot(2,2,2)
df["b"].plot(ax=axes[0,1], kind='bar', grid=True)
df["c"].plot(ax=axes[1,0], kind='bar', grid=True)
df["d"].plot(ax=axes[1,1], kind='bar', grid=True)
ax1.grid(True)
ax1.set_ylabel('Test')
ax1.set_xlabel('Test2')
#ax2.set_ylabel('Test')
如何在我的子图中为条形图添加轴标签?请注意,我在测试时已注释掉 ax2=plt.subplot(2,2,2) ,但这会完全擦除条形图并添加标签。为什么它会这样做,我该如何解决这个问题?下面是带有 ax2... 未注释的输出。
解决方案
据我了解,您不需要ax1
and ax2
。你最好使用轴网格:
fig, axes = plt.subplots(figsize=(8,8),nrows=2, ncols=2)
axes[0,0].plot(x,y)
df["b"].plot(ax=axes[0,1], kind='bar', grid=True)
df["c"].plot(ax=axes[1,0], kind='bar', grid=True)
df["d"].plot(ax=axes[1,1], kind='bar', grid=True)
axes[0,0].grid(True)
axes[0,0].set_ylabel('Test')
axes[0,0].set_xlabel('Test2')
axes[0,1].set_ylabel('Test')
输出:
推荐阅读
- networking - 在连接关闭之前,TcpStream 不会从读取返回
- jquery - 如何使用 jQuery 获取单选按钮的值?
- java - 正则表达式:多行检查问题
- java - 如何检查 Java 应用程序是否在终端窗口中运行(System.in/System.out 可用)
- php - 如何找出 MooTools 停止工作的原因?
- django - 如何在 Django 中的类别下显示子类别?
- google-cloud-platform - 部署后云函数 URL 不起作用
- javascript - 如何在滚动时使用 JavaScript 检测特定的 CSS 类?
- python-3.x - 在 Keras 中加载数据集时如何修复错误?
- flutter - 如何使用密码验证 (dd-mm) 格式的当前日期