【问题标题】:How to display x axis labels on all bar chart subplots?如何在所有条形图子图上显示 x 轴标签?
【发布时间】:2019-01-28 22:19:03
【问题描述】:

问题:

我正在尝试在我的条形图子图上显示所有标签。我已经尝试了 answeranswer 中的所有内容,但没有成功。

数据:

data = StringIO("""

Index-- --Rk    Passing-Player  Passing-Cmp Passing-Att Passing-Pct Passing-Yds Passing-Y/A Passing-AY/A    Passing-TD  Passing-Int Passing-Rate
0   0   1   Patrick Mahomes 364 573 63.5    4653    8.1 8.2 36  15  147.2
1   1   2   Davis Webb  22  41  53.7    300 7.3 8.3 2   0   131.2
2   2   3   Nic Shimonek    1   2   50.0    18  9.0 9.0 0   0   125.6
3   3   4   Jakeem Grant    1   1   100.0   72  72.0    92.0    1   0   1034.8
4   4   5   Jonathan Giles  1   1   100.0   3   3.0 3.0 0   0   125.2
5   5   6   Ian Sadler  0   1   0.0 0   0.0 0.0 0   0   0.0

""")

df = pd.read_table(data, delim_whitespace=True)

dfs = [df] * 9

绘图代码:

fig, axs = plt.subplots(3,3, figsize=(10,10), sharey=True, sharex=False)
for ax,df in zip(axs.ravel(), dfs):
    ax.bar(df['Passing-Player'], df['Passing-Yds'])
    for tick in ax.get_xticklabels():
        tick.set_visible(True)

fig.autofmt_xdate(rotation=90)
fig.tight_layout(pad=2.0, w_pad=2.0, h_pad=10.0)
fig.savefig("tester.png")

结果:

如何显示所有 x 轴标签,因为在我的真实数据中,每个图都因玩家名称不同而不同。

【问题讨论】:

  • 你在运行 python 2.7 吗?

标签: python matplotlib


【解决方案1】:

您需要删除 fig.autofmt_xdate(rotation=90),因为您不使用日期,并使用 ax.set_xticklabels(labels_go_here, rotation=90) 自己设置刻度标签并进行轮换

fig, axs = plt.subplots(3,3, figsize=(10,10), sharey=True, sharex=False)
for ax, df in zip(axs.ravel(), dfs):
    ax.bar(df['Passing-Player'], df['Passing-Yds'])
    ax.set_xticklabels(df['Passing-Player'], rotation=90)

fig.tight_layout(pad=2.0, w_pad=2.0, h_pad=10.0)

plt.show()

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 2021-03-18
    • 2014-02-12
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多