gpt4 book ai didi

python - 在 Python 中创建圆形饼图子图

转载 作者:行者123 更新时间:2023-12-04 18:02:07 25 4
gpt4 key购买 nike

我正在尝试使用 DF 在绘图中创建饼图子图。但是我所有的饼图都不是真正的圆形,而是前两个是椭圆形的。请指导我如何制作相同大小和圆形的所有子图。我正在使用的代码如下所示

fig = plt.figure()
ax1 = plt.subplot(131)
ax2 = plt.subplot(132)
ax3 = plt.subplot(133)

ax1 = test1_pie.plot(kind='pie',y=test1,ax =ax1)
plt.axis('equal')

ax2 = test2_pie.plot(kind='pie',y=test2,ax=ax2)
plt.axis('equal')

ax3 = test3_pie.plot(kind='pie',y=test3,ax=ax3)
plt.axis('equal')

最佳答案

混合状态机不是一个好主意 pyplot调用和法线轴方法调用。这是一个经典的例子。

plt.<whatever>在这种情况下,将引用最后创建的轴。你只打电话 axis('equal')在最后一个轴对象上。

最好坚持使用普通的 axes-method api。

例如:

fig = plt.figure()
ax1 = plt.subplot(131)
ax2 = plt.subplot(132)
ax3 = plt.subplot(133)

ax1 = test1_pie.plot(kind='pie', y=test1, ax=ax1)
ax1.axis('equal')

ax2 = test2_pie.plot(kind='pie', y=test2, ax=ax2)
ax2.axis('equal')

ax3 = test3_pie.plot(kind='pie', y=test3, ax=ax3)
ax3.axis('equal')

作为一个独立的例子:

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=3)

for ax in axes:
x = np.random.random(np.random.randint(3, 6))
ax.pie(x)
ax.axis('equal')

plt.show()

enter image description here

关于python - 在 Python 中创建圆形饼图子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com