python - 使用 matplotlib 增加饼图大小,radius 参数似乎什么都不做

标签 python python-2.7 matplotlib pie-chart

试图把馅饼做大。查看文档和其他地方,它说要设置半径。似乎无论我在半径中输入哪个值都没有增加。我发布了完整的代码和它生成的图像。

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')

enter image description here

最佳答案

如果打开饼图的坐标轴,

ax.pie(..., radius=1800, frame=True)

您会看到半径确实应用正确。

enter image description here

如果你想让轴在图中显得更大,你可以使用 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()

enter image description here

关于python - 使用 matplotlib 增加饼图大小,radius 参数似乎什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49199164/

相关文章:

matplotlib - Jupyter notebook-如何缩放绘图以填充整个页面宽度? (同时保持纵横比)

python - tkinter 不更新值

python - JAX:每个示例的平均梯度与聚合梯度不同

python - 如何使用 python scapy 创建示例 IPSec 数据包

python - 如何在Python中连续连接子列表中的一对坐标?

python-3.x - 随着地球旋转,对两点之间的点进行动画处理

python - Qt5 应用程序中的 matplotlib 矩形选择器图形故障

python - 循环范围结束后自动销毁变量

python - 将 json 文件传递​​给 Python 中的方法

python - 为数组python中的每个元素添加值