【问题标题】:Stacked plot with spaced xticks带有间隔 xticks 的堆积图
【发布时间】:2020-11-17 17:39:48
【问题描述】:

我已经完成了一个聚合,产生了以下数据帧

df2 = tweet.groupby(['Realdate', 'Type'])['Text'].count().unstack().fillna(0)

Type           BLM   Black
Realdate                  
2020-03-01    21.0     9.0
2020-03-02    20.0    13.0
2020-03-03    32.0    16.0
2020-03-04     3.0     9.0
2020-03-05    28.0    16.0
...            ...     ...
2020-07-10  4050.0  4474.0
2020-07-11  2815.0  3743.0
2020-07-12  3575.0  3863.0
2020-07-13  3435.0  4704.0
2020-07-14  3284.0  4352.0

然后我创建了一个堆叠图,如下所示:

df2[['BLM','Black']].plot(kind='bar', stacked=True, figsize=(20,10))

输出是:

我有太多的日子,我正在努力间隔 xticks。有人能帮助我吗? 我很想更换我的 xticks 并生成新的,但到目前为止我没有成功。

非常感谢

【问题讨论】:

    标签: python-3.x matplotlib group-by xticks


    【解决方案1】:

    经过大量研究,我找到了这个答案

    sum_df=tweet.groupby(['Realdate', 'Type'])['Text'].count().unstack().fillna(0)
    sum_df=sum_df.reset_index()
    print(sum_df)
        
    
    fig, ax1 = plt.subplots(figsize=(15, 10))    
    ax1.set_xlabel('Dates')    
    ax1.set_ylabel('# of Tweets', color='b')    
    ax1.yaxis.tick_left()
    sum_df[['Black', 'BLM']].plot( kind='bar', stacked=True, ax=ax1, figsize=(20,10))     
    ax1.legend(loc='upper left', fontsize=8)       
    ax1.set_xticklabels(sum_df.Realdate, rotation=90)    
    for label in ax1.xaxis.get_ticklabels()[::2]:
        label.set_visible(False)    
    plt.legend(['#BlackLivesMatter', '#BLM'])
    plt.title('# of Tweets per Day')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多