Changing line properties in matplotlib pie chart

心已入冬 提交于 2020-07-18 10:31:39

问题


I'm trying to make the lines on my matplotlib pie chart much lighter. Because I have so many slices, the lines are way too thick, as shown here:

enter image description here

I read this example which suggests using rcparam like this:

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

but although I can change the text color, it doesn't change the width of the lines between the pie slices. I believe that's because the slices aren't governed by line objects but by wedge objects. So is there a way to set the wedge border style and color?

Thanks a lot, Alex


回答1:


try this:

ax = plt.subplot(111) 
wedges, texts = ax.pie(np.abs(np.random.randn(5)))

for w in wedges:
    w.set_linewidth(2)
    w.set_edgecolor('cyan')

pie

Additionally, if you only have an axes object and don't have direct access to the pie's wedges you can retrieve the wedges from ax.patches:

wedges = [patch for patch in ax.patches if isinstance(patch, matplotlib.patches.Wedge)]



回答2:


Im coming late to this party, but just found that, for pie charts, instead of using:

plt.rcParams['lines.linewidth'] = 2

you can use:

plt.rcParams['patch.linewidth'] = 0  

Also, to change the pie chart line color use this:

plt.rcParams['patch.edgecolor'] = 'white' 

instead of:

plt.rcParams['line.color'] = 'white'


来源:https://stackoverflow.com/questions/20551477/changing-line-properties-in-matplotlib-pie-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!