2015-08-28 121 views
1

所以我一直在鬼混pie plot()函数,它运行良好,除了当我尝试在楔形标签中使用mathtext时(除非在autopct标签中,我想要号码,一个\下午(+ - )号和一些相关的号码)。是否有可能简单地做到这一点?我是否必须拉出所有楔形对象并操纵它们的文本(可能本身就是一系列Text对象),还是有一些更简单的方法?我不介意一些复杂性,我只是非常希望能够做到这一点。Matplotlib饼图Mathtext标签/ Autopct

编辑:一个例子将是第一个饼图例如在matplotlib代码集:

import matplotlib.pyplot as plt 


# The slices will be ordered and plotted counter-clockwise. 
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' 
sizes = [15, 30, 45, 10] 
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] 
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') 

plt.pie(sizes, explode=explode, labels=labels, colors=colors, 
     autopct='%1.1f%%', shadow=True, startangle=90) 
# Set aspect ratio to be equal so that pie is drawn as a circle. 
plt.axis('equal') 

plt.show() 

的想法是将具有通过用+结束autopct格式关键字生成的号码 - 和一些数。

+1

的你在做什么,一个最小的例子,使问题更容易理解/答案。 – tacaswell

回答

3

我不确定要了解您的问题是否与mathtext或仅创建自定义标签有关。另外,你不会说你在±符号之后获得的价值。

这里我假设你有一个列表,其中包含你想为每个楔子添加的值。

# The slices will be ordered and plotted counter-clockwise. 
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' 
sizes = [15, 30, 45, 10] 
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] 
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') 

patches, texts, autotexts = plt.pie(sizes, explode=explode, labels=labels, colors=colors, 
     autopct='%1.1f%%', shadow=True, startangle=90) 
# Set aspect ratio to be equal so that pie is drawn as a circle. 
plt.axis('equal') 


otherInfo = [3, 5, 3, 4] 

for text, info in zip(autotexts, otherInfo): 
    text.set_text(u"%s ± %.1f" % (text.get_text(), info)) #you can play here with the format to get the label that you want 

enter image description here

+0

嗯,我没有详细说明,因为我只是想知道它是否可以完成。显然,它当然可以。 –