为什么 X 轴上没有显示所有刻度标签?
这是我的代码。
fig, ax1 = plt.subplots()
fig.set_figheight(7)
fig.set_figwidth(12)
ax1.bar(df.index, df['occurence of defects'], color="C0")
ax1.set_ylabel("Qty", color="C0")
ax1.tick_params(axis="y", colors="C0")
ax1.set_xlabel("Defect")
ax1.set_xticklabels(df['Name of Defect'],rotation=45)
ax2 = ax1.twinx()
ax2.plot(df.index, df["cum percentage"], color="C1", marker="D", ms=7)
ax2.yaxis.set_major_formatter(PercentFormatter())
ax2.tick_params(axis="y", colors="C1")
plt.show()
This is my code.
fig, ax1 = plt.subplots()
fig.set_figheight(7)
fig.set_figwidth(12)
ax1.bar(df.index, df['occurence of defects'], color="C0")
ax1.set_ylabel("Qty", color="C0")
ax1.tick_params(axis="y", colors="C0")
ax1.set_xlabel("Defect")
ax1.set_xticklabels(df['Name of Defect'],rotation=45)
ax2 = ax1.twinx()
ax2.plot(df.index, df["cum percentage"], color="C1", marker="D", ms=7)
ax2.yaxis.set_major_formatter(PercentFormatter())
ax2.tick_params(axis="y", colors="C1")
plt.show()
this is ss of output
I made circles where labels are missing. How can I fix that? Even the current labels on the x-axis aren't in their supposed positions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道细节,但它会根据刻度数自动确定图表的比例。在这种情况下,我们将跳过一个。尝试禁用
#ax1.set_xticklabels(df['Name of Defect'],rotation=45)
,您就会明白。如果您指定所需轴的刻度数,它将与标签和显示相匹配。I don't know the details, but it automatically determines the scale of the graph according to the number of ticks. In this case, we are skipping one. Try disabling
#ax1.set_xticklabels(df['Name of Defect'],rotation=45)
and you will understand. If you specify the number of ticks for the axis you need, it will match the label and display.Matplotlib 有这样一个特点,当你调用
set_xticklabels,那么 set_xticks 也应该被调用(以指定
这些标签的放置位置(x 坐标))。
在您的情况下,x标签应放置在每个栏下方,因此
在之前插入::
原因是通常x标签不应该放置在每个
x 坐标,尤其是当绘图类型不是 bar 时。
另一个提示:由于您的 x 标签很长,请考虑旋转
90 或其他,但大于 45。
Matplotlib has such a characteristics, that when you invoke
set_xticklabels, then set_xticks should also be invoked (to specify
where these labels are to be placed (at which x coordinates)).
In your case x labels should be placed below each bar, so insert:
before:
The reason is that quite often x labels should be placed not at each
x coordinate, especially when the plot type is other than bar.
And another hint: Since your x labels are quite long, consider rotation
of 90, or other, but greater than 45.