【问题标题】:Prevent overlapping labels in grouped bar chart防止分组条形图中的重叠标签
【发布时间】:2022-09-25 07:45:49
【问题描述】:

我正在使用以下函数绘制一个分组条形图,比较使用不平衡和平衡数据构建的模型的性能。

# plot results to compare between balanced and imbalanced data

def barChartBalancing(imbalancedResults, rusResults, smoteResults, score, title, string):
    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np

    #make font bigger
    font = {\'size\'   : 15}

    matplotlib.rc(\'font\', **font)
    
    labels = names
    
    x = np.arange(len(labels))  # the label locations
    width = 0.2  # the width of the bars

    fig, ax = plt.subplots(figsize=(10,9))
    rects1 = ax.bar(x - width, imbalancedResults, width, label=\'Imbalanced Dataset\')
    rects2 = ax.bar(x , rusResults, width, label=\'RandomUnderSampler\')
    rects3 = ax.bar(x + width, smoteResults, width, label=\'SMOTE\')
    

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel(score)
    ax.set_title(title)
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend(loc=\'upper center\')
    

    ax.bar_label(rects1, padding=5, fmt=\'%.2f\', label_type=\'edge\')
    ax.bar_label(rects2, padding=5, fmt=\'%.2f\', label_type=\'edge\')
    ax.bar_label(rects3, padding=5, fmt=\'%.2f\', label_type=\'edge\')

    fig.tight_layout()
    
    fileName = string +\'.png\'
    print(fileName)
    plt.savefig(\'figures/resampling/\' + fileName)
    
    plt.show()

但是,当我运行它时,分组条上方的标签是重叠的,如下所示:

我尝试更改 \'padding\' 值,但后来意识到这是条形图的末端和末端之间的距离。我还尝试根据 Rabinzel 的评论在 bar_label 中使用 fontsize 参数更改字体大小,这很有帮助,但我必须使字体非常小以防止完全重叠,以至于很难阅读.

有什么想法可以解决重叠问题吗?我想知道我是否可以将标签垂直放置在栏上?我认为这可以解决问题,而不必使用极小的字体。

  • 您是否尝试将 fontsize 作为参数添加到 ax.bar_label ?我认为它应该工作。您还可以旋转标签以消除重叠
  • 我刚刚尝试过,它确实有帮助,但我必须使字体非常小以防止完全重叠,以至于很难阅读。知道我是否可以将标签垂直放置在栏上吗?我认为这将完全解决问题,而不必使用极小的字体。

标签: python matplotlib


【解决方案1】:

我不知道您是否考虑过水平绘制图形,但是通过这种方法,您将能够:

  • 有更长的名称作为标签
  • 您不会遇到重叠问题。
  • 同一图中的模型(条目)数量不限。
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

# Example data
models = ('Linear regresion', 'Singular vector machine', 'linear vector clasification',
          'Naive Bayes Classifier', 'Gradient boosting')
y_pos = np.arange(len(models))
distance = 3 + 10 * np.random.rand(len(models))
speed = 3 + 10 * np.random.rand(len(models))
age = 3 + 10 * np.random.rand(len(models))
width = 0.2 

rects1=ax.barh(y_pos-width, distance, width, align='center', label="Unvalance dataset")
rects2=ax.barh(y_pos, speed, width, align='center', label="Random under sapler")
rects3=ax.barh(y_pos + width, age, width, align='center', label="SMOTE")
ax.set_yticks(y_pos, labels=people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.bar_label(rects1, padding=5, fmt='%.2f', label_type='edge')
ax.bar_label(rects2, padding=5, fmt='%.2f', label_type='edge')
ax.bar_label(rects3, padding=5, fmt='%.2f', label_type='edge')
plt.legend()
plt.xlim(0,15)
plt.show()

【讨论】:

  • 这很聪明!没想到。
【解决方案2】:

以下是更改字体大小和旋转bar_label 的方法:

其中之一的示例:

ax.bar_label(rects1, padding=5, fmt='%.2f', label_type='edge', fontsize=9, rotation='vertical')

如果您想进行不同的旋转,也可以将整数传递给旋转:rotation=45

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多