【问题标题】:pandas matplotlib x-axis only labels every fifth location [duplicate]pandas matplotlib x轴仅每五个位置标记一次[重复]
【发布时间】:2019-11-03 19:13:54
【问题描述】:

我在 matplotlib 中绘制一个条形图,其中包括从具有相同 x 轴的 3 个不同数据帧中的一列中获取数据并将条形图并排绘制

除了 x 轴只标记每五个位置而不是所有位置之外,它大部分都有效?如何标记每个位置?

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(25,10))

plt.subplots_adjust(hspace=0.8, wspace=0.2)


x = df_rightmoveSmry.index

labels = list(df_rightmoveSmry['Adj Date'].astype(str).str[0:10])

y1 = df_rightmoveSmry['branches']
y2 = df_zooplaSmry['branches']
y3 = df_onthemarketSmry['branches']
ax.set_title('Number of estate agents on each Platform', fontsize=20)
ax.set_xticklabels(labels, rotation = 90, fontsize=20)
ax.yaxis.set_tick_params(labelsize=15)
ax.bar(x, y1, width=0.3)
ax.bar(x+0.3, y2, width=0.3)
ax.bar(x+0.6, y3, width=0.3)

给予:

【问题讨论】:

  • 切勿在未设置标签位置的情况下使用set_xticklabels,例如通过set_xticks!
  • @ImportanceOfBeingErnest 成功了,谢谢!对于其他人,我添加的代码行是ax.set_xticks(x)

标签: python pandas matplotlib


【解决方案1】:

不妨试试:

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(25,10))

plt.subplots_adjust(hspace=0.8, wspace=0.2)


x = df_rightmoveSmry.index

labels = list(df_rightmoveSmry['Adj Date'].astype(str).str[0:10])

y1 = df_rightmoveSmry['branches']
y2 = df_zooplaSmry['branches']
y3 = df_onthemarketSmry['branches']
ax.set_title('Number of estate agents on each Platform', fontsize=20)
ax.set_xticks(list(df_rightmoveSmry['Adj Date'].unique())) # <--- this line added
ax.set_xticklabels(labels, rotation = 90, fontsize=20)
ax.yaxis.set_tick_params(labelsize=15)
ax.bar(x, y1, width=0.3)
ax.bar(x+0.3, y2, width=0.3)
ax.bar(x+0.6, y3, width=0.3)

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 2023-01-05
    • 1970-01-01
    • 2014-03-13
    • 2011-09-03
    • 2015-10-10
    • 2023-04-04
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多