How to avoid pie chart labels overlapping in MatPlotLib ver.2.0.2?(如何避免饼图标签在 MatPlotLib ver.2.0.2 中重叠?)
问题描述
There were a lot of questions posted regarding labels overlap for pie chart plotting. However, I couldn't find automated solution except for converting them to the legend. This solution doesn't work for me as I have a lot of values (around 60), and conversion to a legend will make the plot look very messy and unclear. So my question, if I want to label pie wedges around the pie, is the any automated solution for MatPlotLib version 2.0.2 that enables labels to have a good spacing (no overlapping) ? The only solution I found is to do it manually with annotation(). Please see below the script with dummy values. Also is it possible to connect the wedge of the pie to the related label with an arrow?
I use Python 2.7 and MatPlotLib 2.0.2
Thanks,
Example 1 (overlapping labels)
Example 2 (manually corrected)
import pylab
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
l = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1],
#labels=("one","two","three made up sentences","four is also ther","five becomes a sentence","six it is","seven long", "eight long sent", "nine, as bla bel mo","ten is also short"),
labels=("","","","","","six it is","seven long", "eight long sent", "nine, as bla bel mo","ten is also short"),
colors=("b","g","r","y", "b","g","r","y","g","black"),
startangle =20,
radius=1,
frame=True, # Plot axes frame with the chart if true.
labeldistance = 1.1 ) #returns a list of matplotlib.patches.Wedge objects
l2 = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1],
colors=("r","g","b","w", "g","b","y","r","w","black"),
startangle =20,
radius=1-0.7,
frame=True) # Plot axes frame with the chart if true.
coor = [t.get_position() for t in l[1]]
ax.axis('equal')
plt.annotate( 'one was very short now ext', xy= (coor[0][0], coor[0][1]) ) # https://kite.com/docs/python/matplotlib.pyplot.annotate
plt.annotate( 'two long sentense', xy= (coor[1][0], coor[1][1]) )
plt.annotate('three things to say' , xy= (coor[2][0], coor[2][1]+0.02) )
plt.annotate( 'four main tasks to do', xy= (coor[3][0], coor[3][1]+0.04) )
plt.annotate( 'five reasons to avoid', xy= (coor[4][0], coor[4][1]+0.06 ))
plt.savefig('test_draft.pdf')
plt.show()
You could experiment with rotating the text. The following determines an angle for each wedge and then rotates the annotation text accordingly. Some extra tweaks are needed depending on the angle to determine the text alignment and also to ensure the text does not appear upside down. You might need to further refine this.
import pylab
import matplotlib.pyplot as plt
import math
fig, ax = plt.subplots()
labels= [
"one",
"two",
"three made up sentences",
"four is also there",
"five becomes a sentence",
"six it is",
"seven long",
"eight long sent",
"nine, as bla bel mo",
"ten is also short"]
l = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],
labels=[''] * len(labels),
colors=("b","g","r","y", "b","g","r","y","g","black"),
startangle=20,
radius=1,
frame=True, # Plot axes frame with the chart if true.
labeldistance=1.1) #returns a list of matplotlib.patches.Wedge objects
l2 = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1],
colors=("r","g","b","w", "g","b","y","r","w","black"),
startangle=20,
radius=1-0.7,
frame=True) # Plot axes frame with the chart if true.
for label, t in zip(labels, l[1]):
x, y = t.get_position()
angle = int(math.degrees(math.atan2(y, x)))
ha = "left"
va = "bottom"
if angle > 90:
angle -= 180
if angle < 0:
va = "top"
if -45 <= angle <= 0:
ha = "right"
va = "bottom"
plt.annotate(label, xy=(x,y), rotation=angle, ha=ha, va=va, size=8)
ax.axis('equal')
plt.show()
Which would display as:
这篇关于如何避免饼图标签在 MatPlotLib ver.2.0.2 中重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何避免饼图标签在 MatPlotLib ver.2.0.2 中重叠?
基础教程推荐
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
