【问题标题】:Matplotlib bar chart customisation for multiple valuesMatplotlib 条形图自定义多个值
【发布时间】:2017-01-15 07:31:34
【问题描述】:

我有一个包含国家/地区及其出现次数的元组列表。我有 175 个国家/地区都有长名称。

当我绘制它们时,我得到:

如您所见,所有内容都非常拥挤,没有空间,您几乎无法阅读任何内容。

我使用的代码(原始数据文件很大,但这包含我的 matplotlib 特定代码):

def tupleCounts2Percents(inputList):
     total = sum(x[1] for x in inputList)*1.0
     return [(x[0], 1.*x[1]/total) for x in inputList]

def autolabel(rects,labels):
# attach some text labels
    for i,(rect,label) in enumerate(zip(rects,labels)):
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            label,
            ha='center', va='bottom',fontsize=6,style='italic')

def countryChartList(inputlist,path):
    seen_countries = Counter()

    for dict in inputlist:
        seen_countries += Counter(dict['location-value-pair'].keys())

    seen_countries = seen_countries.most_common()

    seen_countries_percentage = map(itemgetter(1), tupleCounts2Percents(seen_countries))
    seen_countries_percentage = ['{:.2%}'.format(item)for item in seen_countries_percentage]

    yvals = map(itemgetter(1), seen_countries)
    xvals = map(itemgetter(0), seen_countries)

    plt.figure()
    countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9)
    plt.xticks(range(len(seen_countries)), xvals,rotation=90)

    plot_margin = 0.25
    x0, x1, y0, y1 = plt.axis()
    plt.axis((x0,
              x1,
              y0,
              y1+plot_margin))

    plt.title('Countries in Dataset')
    plt.xlabel('Countries in Data')
    plt.ylabel('Occurrences')

    plt.tick_params(axis='both', which='major', labelsize=6)
    plt.tick_params(axis='both', which='minor', labelsize=6)
    plt.tight_layout()

    autolabel(countrychart,seen_countries_percentage)

    plt.savefig(path)
    plt.clf()

我输入的字典是什么样子的:

    list = [
    {
        "location-value-pair": {
            "Austria": 234
        }
    },
    {
        "location-value-pair": {
            "Azerbaijan": 20006.0
        }
    },
    {
        "location-value-pair": {
            "Germany": 4231
        }
    },
    {
        "location-value-pair": {
            "United States": 12121
        }
    },
    {
        "location-value-pair": {
            "Germany": 65445
        }
    },
    {
        "location-value-pair": {
            "UK": 846744
        }
    }
}
]

我该怎么做:

  1. 制作东西以便人们可以阅读它们 - 答案会是带箱的直方图而不是条形图吗?也许每 10% 踩一次?
  2. 如何使刻度标签和条形上方的标签(百分比)不重叠?
  3. 欢迎任何其他见解(例如渐变色条,从红色到黄色)?

编辑

我将国家/地区的数量减少到前 50 个,使条形更加透明,并将刻度更改为旋转 45 度。我仍然发现第一个条有一个横穿 y 轴的刻度线是不可读的。我该如何更改?

autolabel 函数中更改为countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9,alpha=0.6)rotation=45.text() 参数。

【问题讨论】:

  • @Kaël 我已经旋转了 90 度 - 请参阅代码 :)

标签: python matplotlib plot charts figure


【解决方案1】:

问题在于自动标签的对齐方式:

def autolabel(rects,labels):
# attach some text labels
    for i,(rect,label) in enumerate(zip(rects,labels)):
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            label,
            ha='center', va='bottom',fontsize=6,style='italic')

改为:

def autolabel(rects,labels):
# attach some text labels
    for i,(rect,label) in enumerate(zip(rects,labels)):
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            label,
            ha='left', va='bottom',fontsize=6,style='italic', rotation=45)

获得:

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2020-04-10
    • 1970-01-01
    • 2022-09-25
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多