Increasing pie chart size with matplotlib, radius parameter appears to do nothing(使用matplotlib增加饼图大小,半径参数似乎不起作用)
本文介绍了使用matplotlib增加饼图大小,半径参数似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试着把馅饼做大。看着文件和其他地方,它说设置半径。似乎无论我在半径中放入哪个值,都不会增加。我发布了完整的代码和它生成的图像。
import matplotlib.pyplot as plt
def autopct_generator(limit):
"""Remove percent on small slices."""
def inner_autopct(pct):
return ('%.2f%%' % pct) if pct > limit else ''
return inner_autopct
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs', 'Test', 'Test2', 'Test3',
'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9', 'Test10',
'Test11', 'Test12', 'Test13', 'Test14'
sizes = [15, 30, 45, 10, 10, 24, 13, 18, 28, 20, 13, 15, 5, 1, 18, 10,
10, 10]
NUM_COLORS = len(sizes)
fig1, ax1 = plt.subplots(figsize=(6, 5))
# set color theme
# https://matplotlib.org/api/pyplot_summary.html#colors-in-matplotlib
theme = plt.get_cmap('bwr')
ax1.set_color_cycle([theme(
1. * i / NUM_COLORS) for i in range(NUM_COLORS)])
box = ax1.get_position()
ax1.set_position([box.x0, box.y0, box.width * 1.3, box.height])
_, _, autotexts = ax1.pie(
sizes, autopct=autopct_generator(7), startangle=90, radius=1.8 * 1000)
for autotext in autotexts:
autotext.set_weight('bold')
ax1.axis('equal')
total = sum(sizes)
plt.legend(
loc='upper left',
labels=['%s, %1.1f%%' % (
l, (float(s) / total) * 100) for l, s in zip(labels, sizes)],
prop={'size': 12},
bbox_to_anchor=(0.0, 1),
bbox_transform=fig1.transFigure
)
# fig1.set_size_inches(18.5, 10.5)
fig1.savefig('chart.png')
推荐答案
如果打开饼图的轴,
ax.pie(..., radius=1800, frame=True)
您将看到半径确实被正确应用。
如果要让轴在绘图中显示得更大,可以使用subplot parameters。
fig.subplots_adjust(left,bottom,right,top)
示例代码:
import matplotlib.pyplot as plt
sizes = [15, 30, 45, 10, 10, 24, 13, 18, 28, 20, 13, 15, 5, 1, 18, 10,
10, 10]
labels = ["Frogs %s" % i for i in sizes]
fig1, ax1 = plt.subplots(figsize=(6, 5))
fig1.subplots_adjust(0.3,0,1,1)
theme = plt.get_cmap('bwr')
ax1.set_prop_cycle("color", [theme(1. * i / len(sizes)) for i in range(len(sizes))])
_, _ = ax1.pie(sizes, startangle=90)
ax1.axis('equal')
total = sum(sizes)
plt.legend(
loc='upper left',
labels=['%s, %1.1f%%' % (
l, (float(s) / total) * 100) for l, s in zip(labels, sizes)],
prop={'size': 11},
bbox_to_anchor=(0.0, 1),
bbox_transform=fig1.transFigure
)
plt.show()
这篇关于使用matplotlib增加饼图大小,半径参数似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:使用matplotlib增加饼图大小,半径参数似乎不起作用
基础教程推荐
猜你喜欢
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
