如何改善pyplot饼图中的分数标签的旋转 [英] How can I improve the rotation of fraction labels in a pyplot pie chart

查看:65
本文介绍了如何改善pyplot饼图中的分数标签的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我复制了以下示例代码,并稍作改动.我想以某个角度旋转分数.我实现了目标,但我的问题是是否有一种更简单的方式来旋转分数:

I copied the following example code with a minor change. I want to rotate the fractions in a certain angle. I achived my goal but my question is if there is an easier way to rotate the fractions:

import matplotlib.pyplot as plt
import matplotlib

# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)  # explode 1st slice

# Plot
pie_properties = plt.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', shadow=False, startangle=140, pctdistance=0.8, radius = 0.5)

# Rotate fractions
# [0] = wedges, [1] = labels, [2] = fractions
fraction_text_list = pie_properties[2]
for text in fraction_text_list:
    text.set_rotation(315)

plt.axis('equal')
plt.show()

有可能对此进行改善吗?

Is it possible to improve this?

推荐答案

问题中旋转自动百分比标签的方法已经很简单了.如果用更轻松"的意思是较短",则可以将整个命令放在一行中:

The method in the question to rotate the auto percentage labels is already quite easy. If by "easier" you mean "shorter", you can put the whole command in one single line:

import matplotlib.pyplot as plt

# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)  # explode 1st slice

# Plot
w,l,p = plt.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140, pctdistance=0.8, radius = 0.5)
[t.set_rotation(315) for t in p]

plt.axis('equal')
plt.show()

这篇关于如何改善pyplot饼图中的分数标签的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆