为 Matplotlib 饼图中的数据指定特定颜色 [英] Assign specific colours to data in Matplotlib pie chart

查看:116
本文介绍了为 Matplotlib 饼图中的数据指定特定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib 创建饼图,其中每个类别的颜色都是固定的.

I'm trying to create pie charts with matplotlib in which the colour of each category is fixed.

我有一个函数可以根据值和类别数据集创建饼图.举个例子:

I've got a function which creates a pie chart from sets of value and category data. Here's one example:

Category     Value
TI           65
Con          43
FR           40
TraI         40
Bug          38
Data         22
Int          15
KB           12
Other        8
Dep          7
PW           6
Uns          5
Perf         4
Dep          3

问题是数据从一个实例到另一个实例不同,进而改变了类别的顺序.因此,每次我生成图表时,每个类别都会被标记为不同的颜色.我每次都可以按字母顺序对数据进行排序,但这会导致两个问题:某些数据集中缺少某些类别,而且我更喜欢按大小排序,以便最小的楔子水平定向.

The problem is that the data differs from one instance to another, and that in turn changes the order of the categories. Thus, each category gets labelled a different colour each time I generate a chart. I could sort the data alphabetically every time, but that causes two problems: some categories are missing from some datasets, and I'd prefer it sorted by size anyway so that the smallest wedges are oriented horizontally.

如何设置 matplotlib 以根据 pandas.Series 的索引分配颜色?

How can I set matplotlib to assign colours depending on, say, the index of a pandas.Series?

这是我用来生成饼图的代码:

Here's the code that I'm using to generate a pie chart:

import matplotlib.pyplot as plt

slices = [62, 39, 39, 38, 37, 21, 15,  9,  6,  7,  6,  5,  4, 3]

cmap = plt.cm.prism
colors = cmap(np.linspace(0., 1., len(slices)))

labels = [u'TI', u'Con', u'FR', u'TraI', u'Bug', u'Data', u'Int', u'KB', u'Other', u'Dep', u'PW', u'Uns', u'Perf', u'Dep']

fig = plt.figure(figsize=[10, 10])
ax = fig.add_subplot(111)

pie_wedge_collection = ax.pie(slices, colors=colors, labels=labels, labeldistance=1.05, autopct=make_autopct(slices))

for pie_wedge in pie_wedge_collection[0]:
    pie_wedge.set_edgecolor('white')

titlestring = 'Issues'

ax.set_title(titlestring)

我忘了解释 autopct 函数,它用于添加值和百分比标签:

I forgot to explain the autopct function, it's for adding value and percentage labels:

def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{p:.2f}%  ({v:d})'.format(p=pct,v=val)
    return my_autopct

推荐答案

这里有一个你可以尝试的想法.根据您的标签和颜色制作字典,以便将每种颜色映射到一个标签.然后,在制作饼图后,使用此字典分配楔形的facecolor.

Here's an idea you could try. Make a dictionary from your labels and colors, so each color is mapped to a label. Then, after making the pie chart, go in an assign the facecolor of the wedge using this dictionary.

这里有一段未经测试的代码,可以满足您的要求:

Here's an untested bit of code which might do what you are looking for:

import numpy as np
import matplotlib.pyplot as plt

def mypie(slices,labels,colors):

    colordict={}
    for l,c in zip(labels,colors):
        print l,c
        colordict[l]=c

    fig = plt.figure(figsize=[10, 10])
    ax = fig.add_subplot(111)

    pie_wedge_collection = ax.pie(slices, labels=labels, labeldistance=1.05)#, autopct=make_autopct(slices))

    for pie_wedge in pie_wedge_collection[0]:
        pie_wedge.set_edgecolor('white')
        pie_wedge.set_facecolor(colordict[pie_wedge.get_label()])

    titlestring = 'Issues'

    ax.set_title(titlestring)

    return fig,ax,pie_wedge_collection

slices = [37, 39, 39, 38, 62, 21, 15,  9,  6,  7,  6,  5,  4, 3]
cmap = plt.cm.prism
colors = cmap(np.linspace(0., 1., len(slices)))
labels = [u'TI', u'Con', u'FR', u'TraI', u'Bug', u'Data', u'Int', u'KB', u'Other', u'Dep', u'PW', u'Uns', u'Perf', u'Dep']

fig,ax,pie_wedge_collection = mypie(slices,labels,colors)

plt.show()

这篇关于为 Matplotlib 饼图中的数据指定特定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆