在使用matplotlib绘制饼图时,可能会遇到使用wedgeprops参数设置牵引线粗细无效的问题。这可能是因为wedgeprops参数中的linewidth属性被其他参数覆盖了。

解决方法:

  1. 将linewidth属性放在最后一个参数中:
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
colors = ['red', 'green', 'blue', 'yellow']
explode = (0.1, 0, 0, 0)

fig, ax = plt.subplots()

wedgeprops = {'linewidth': 2, 'edgecolor': 'white'}
ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, **wedgeprops)

ax.axis('equal')

plt.show()
  1. 使用字典方式传递wedgeprops参数:
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
colors = ['red', 'green', 'blue', 'yellow']
explode = (0.1, 0, 0, 0)

fig, ax = plt.subplots()

wedgeprops = {'linewidth': 2, 'edgecolor': 'white'}
ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, wedgeprops=wedgeprops)

ax.axis('equal')

plt.show()

另外,还可以使用set_linewidth()方法来设置线条宽度:

import matplotlib.pyplot as plt

labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
colors = ['red', 'green', 'blue', 'yellow']
explode = (0.1, 0, 0, 0)

fig, ax = plt.subplots()

wedges, _, _ = ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)

for wedge in wedges:
    wedge.set_linewidth(2)

ax.axis('equal')

plt.show()

以上两种方法都可以设置饼图牵引线的粗细。

matplotlib饼图牵引线粗细设置:解决wedgeprops失效问题

原文地址: https://www.cveoy.top/t/topic/oWxx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录