我不知道细节,但它会根据刻度数自动确定图形的比例。在这种情况下,我们将跳过一个。尝试禁用#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()