Matplotlib条形图自定义多个值

时间:2016-09-07 14:33:56

标签: python matplotlib plot charts figure

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

当我绘制图表时,我得到:

enter image description here

正如你所看到的,一切都很紧凑,没有空间,你几乎看不到任何东西。

我使用的代码(原始数据文件很大,但这包含我的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. 任何其他见解欢迎(例如,渐变色的条形,红色到黄色)?
  4. 修改

    我将国家/地区的数量减少到前50名,使条形图更加透明,并将标记更改为旋转45度。我仍然发现第一个条形有一个穿过y轴的刻度线,它是不可读的。我怎么能改变这个?

    enter image description here

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

1 个答案:

答案 0 :(得分: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)

获得:

enter image description here