matplotlib:控制饼图字体颜色,线宽

时间:2009-12-16 16:31:30

标签: matplotlib font-size

我正在使用一些简单的matplotlib函数来绘制饼图:

f =数字(...) 馅饼(压裂,爆炸=爆炸,......)

但是,我无法找到如何设置默认字体颜色,线条颜色,字体大小 - 或将它们传递给pie()。怎么做?

3 个答案:

答案 0 :(得分:15)

派对迟到了但我遇到了这个问题并且不想改变我的rcParams。

您可以通过保留从创建饼图返回的文本并使用matplotlib.font_manager正确修改它们来调整标签或自动百分比的文本。

您可以在此处阅读有关使用matplotlib.font_manager的更多信息: http://matplotlib.sourceforge.net/api/font_manager_api.html

内置字体大小列在api中; “大小:'xx-small','x-small','small','medium','large','x-large','xx-large'或绝对字体大小的相对值,例如12"

from matplotlib import pyplot as plt
from matplotlib import font_manager as fm

fig = plt.figure(1, figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
plt.title('Raining Hogs and Dogs')

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]

patches, texts, autotexts = ax.pie(fracs, labels=labels, autopct='%1.1f%%')

proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)

plt.show()

alt text

答案 1 :(得分:11)

可以使用rcParams字典调整全局默认颜色,线宽,尺寸等:

import matplotlib
matplotlib.rcParams['text.color'] = 'r'
matplotlib.rcParams['lines.linewidth'] = 2

可以找到完整的参数列表here.

绘制饼图后,您还可以调整线宽:

from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,8))
pieWedgesCollection = plt.pie([10,20,50,20],labels=("one","two","three","four"),colors=("b","g","r","y"))[0] #returns a list of matplotlib.patches.Wedge objects
pieWedgesCollection[0].set_lw(4) #adjust the line width of the first one.

不幸的是,我无法想出从pie方法或Wedge对象调整饼图标签的字体颜色或大小的方法。查看axes.py的源代码(matplotlib 99.1上的第4606行),它们是使用Axes.text方法创建的。此方法可以采用颜色和大小参数,但目前尚未使用。如果不编辑源代码,您唯一的选择可能就是如上所述全局完成。

答案 2 :(得分:2)

matplotlib.rcParams['font.size'] = 24

确实会更改饼图标签字体大小