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

Sam*_*Sam 13 matplotlib font-size

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

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

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

小智 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()
Run Code Online (Sandbox Code Playgroud)

替代文字


Mar*_*ark 11

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

import matplotlib
matplotlib.rcParams['text.color'] = 'r'
matplotlib.rcParams['lines.linewidth'] = 2
Run Code Online (Sandbox Code Playgroud)

可在此处找到完整的参数列表.

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

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.
Run Code Online (Sandbox Code Playgroud)

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