为什么 X 轴上没有显示所有刻度标签?

发布于 2025-01-10 07:55:58 字数 703 浏览 0 评论 0原文

这是我的代码。

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()

这是输出的 ss 输入图片此处描述我在缺少标签的地方画了圈。我该如何解决这个问题?即使 x 轴上的当前标签也没有处于其假定的位置。

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
enter image description here
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2025-01-17 07:55:59

我不知道细节,但它会根据刻度数自动确定图表的比例。在这种情况下,我们将跳过一个。尝试禁用#ax1.set_xticklabels(df['Name of Defect'],rotation=45),您就会明白。如果您指定所需轴的刻度数,它将与标签和显示相匹配。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
import numpy as np

df = pd.DataFrame({'Name of Defect':list('ABCDEFGHIJKLMNOP'), 'occurence of defects':np.random.randint(1,10,16)})

df['cum'] = df['occurence of defects'].cumsum()
df.sort_values('occurence of defects', ascending=False, ignore_index=True, inplace=True)
df['per'] = df['cum'].apply(lambda x: x / df['cum'].sum())
df['cum percentage'] = df['per'].cumsum()

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_xticks(np.arange(0,16))
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()

输入图片此处描述

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.

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
import numpy as np

df = pd.DataFrame({'Name of Defect':list('ABCDEFGHIJKLMNOP'), 'occurence of defects':np.random.randint(1,10,16)})

df['cum'] = df['occurence of defects'].cumsum()
df.sort_values('occurence of defects', ascending=False, ignore_index=True, inplace=True)
df['per'] = df['cum'].apply(lambda x: x / df['cum'].sum())
df['cum percentage'] = df['per'].cumsum()

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_xticks(np.arange(0,16))
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()

enter image description here

森林散布 2025-01-17 07:55:59

Matplotlib 有这样一个特点,当你调用
set_xticklabels,那么 set_xticks 也应该被调用(以指定
这些标签的放置位置(x 坐标))。

在您的情况下,x标签应放置在每个栏下方,因此

ax1.set_xticks(df.index)

在之前插入::

ax1.set_xticklabels(df['Name of Defect'], rotation=45)

原因是通常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:

ax1.set_xticks(df.index)

before:

ax1.set_xticklabels(df['Name of Defect'], rotation=45)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文