为Matplotlib饼图中的数据分配特定的颜色 [英] Assign specific colours to data in Matplotlib pie chart
问题描述
我正在尝试使用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.
如何根据pandas.Series
的索引设置matplotlib
来分配颜色?
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.
这是一段 unested 的代码,可能会满足您的需求:
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屋!