【问题标题】:How to change the size and format of the text above a barplot in matplotlib?如何更改matplotlib中条形图上方文本的大小和格式?
【发布时间】:2017-07-05 00:24:51
【问题描述】:

我使用以下代码绘制了条形图:

ax=max_runs.sort_values(ascending=False)[:10].plot.bar(width=0.8,color='R') 对于 ax.patches 中的 p: ax.annotate(format(p.get_height()), (p.get_x(), p.get_height()+0.25),color='B')

现在条形图上显示的值是浮点格式。如何将其更改为 int 格式?以及如何减小文本的大小?

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    试试:

    # Get current axis.
    import matplotlib.pyplot as plt 
    ax = plt.gca()  
    
    # Change the labels.
    new_labels = [int(label) for label in ax.get_xticklabels()]
    ax.set_xticklabels(new_labels, fontsize=your_fontsize)
    

    如果您的标签在 x 轴上,如果它们在 y 轴上,则使用 ax.get_yticklabels()ax.set_yticklabels()

    【讨论】:

      猜你喜欢
      • 2013-06-11
      • 2017-11-21
      • 2023-01-07
      • 1970-01-01
      • 2010-10-17
      • 2015-07-12
      • 2017-06-08
      • 2011-04-23
      • 2017-08-31
      相关资源
      最近更新 更多