python - 如何使用子图摆脱 y 轴上不必要的值?
问题描述
我有这两个数据库(df2020 和 data2020):
我用这段代码做了两个子图:
f, axs = plt.subplots(2,2,figsize=(15,5))
X1 = df2020['month']
Y1 = df2020['notes']
ticks = ['1', '2', '3', '4', '5']
ax = f.add_subplot(121)
ax.bar(X1, Y1, facecolor = '#9999ff', edgecolor = 'white')
plt.setp(ax.get_xticklabels(), rotation=45)
ax.set_ylabel('Ratings')
ax.set_title('Evolution des notes durant 2020');
for x,y in zip(X1,Y1):
plt.text(x, y, y, ha='center', va= 'bottom')
X2 = data2020['month']
Y2 = data2020['comms']
ax2 = f.add_subplot(122)
ax2.bar(X2, Y2, facecolor = '#9999ff', edgecolor = 'white')
plt.setp(ax2.get_xticklabels(), rotation=45)
ax2.set_ylabel('Ratings')
ax2.set_title('Evolution du nombre de commentaires durant 2020');
for x,y in zip(X2,Y2):
plt.text(x, y, y, ha='center', va= 'bottom')
这是输出:
但正如你所看到的,这张图有一个小问题,我有点理解为什么。在 x 轴(0.0 到 1.0)和 y 轴(0.0 到 1.0)和右侧一个 0 以及两个图形上都有某种前通用值。我不知道如何摆脱它们,我试图指定set_ylim
或set_ytickslabel
仅更改我想保留的值(y 轴上的 0.0 到 5.0 值和 x 轴上的日期)。
我应该怎么做才能解决这个问题?
谢谢 :)
解决方案
您首先使用 创建四个(空)子图f, axs = plt.subplots(2, 2, figsize=(15, 5))
,然后使用在它们之上创建新的子图f.add_subplot
。
您可以立即使用axs
创建的变量plt.subplots
来绘制数据,之后您不需要明确创建子图。请注意,您当前正在创建一个 2×2 网格的子图,而您想要创建一个 1×2 网格(行)。
f, axs = plt.subplots(1, 2, figsize=(15, 5))
X1 = df2020['month']
Y1 = df2020['notes']
ticks = ['1', '2', '3', '4', '5']
axs[0].bar(X1, Y1, facecolor = '#9999ff', edgecolor = 'white')
plt.setp(axs[0].get_xticklabels(), rotation=45)
axs[0].set_ylabel('Ratings')
axs[0].set_title('Evolution des notes durant 2020')
for x,y in zip(X1,Y1):
axs[0].text(x, y, y, ha='center', va= 'bottom')
X2 = data2020['month']
Y2 = data2020['comms']
axs[1].bar(X2, Y2, facecolor = '#9999ff', edgecolor = 'white')
plt.setp(axs[1].get_xticklabels(), rotation=45)
axs[1].set_ylabel('Ratings')
axs[1].set_title('Evolution du nombre de commentaires durant 2020')
for x,y in zip(X2,Y2):
axs[1].text(x, y, y, ha='center', va= 'bottom')
推荐阅读
- docker - 在 docker-compose 中使用 ssh 密钥文件作为环境变量
- c# - Unity3d构建错误重复类com.google.unity.ads
- c++ - 使用 .natvis 文件在 VS Code 中可视化 C++ 对象
- stripe-payments - 我可以在验证域后删除 Apple 域验证文件吗?
- c - 我在测试用例中得到了一些错误的答案,
- c# - 有什么方法可以定义在渲染 img 标签时在哪里查找图像?
- javascript - 如何使子行列可点击?
- sql - 如何跳过 INSERT 语句中引发的异常?
- python - 获取 FigureWidget 的 plotly.graph_objs 属性错误
- numpy - 使用 numpy 计算 svd 中的奇异值