Python 如何使用子图消除y轴上不必要的值?
我有两个数据库(df2020和data2020): 我用这个代码做了两个子图:Python 如何使用子图消除y轴上不必要的值?,python,python-3.x,matplotlib,axis,subplot,Python,Python 3.x,Matplotlib,Axis,Subplot,我有两个数据库(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')
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创建四个(空)子批。子批(2,2,figsize=(15,5))
,然后使用f.add\u子批在它们上面创建新的子批
您可以立即使用由plt.subplot
创建的axs
变量来绘制数据,之后无需显式创建子绘图。请注意,当前正在创建子地块的2×2栅格,而您希望创建1×2栅格(行)
太棒了,这就是工作!只有一件事,我想你忘了(不管你刚刚编辑了你的帖子)。谢谢你的帮助。我不会忘记在通过plt.subplot
创建之后立即使用axs
。非常感谢:)
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')