python - 在 matplotlib 中绘制饼图

标签 python matplotlib

我拼命想在这个饼图周围添加一个“深色”边框。我在这里尝试了很多问题中描述的解决方案,但结果都没有增加任何内容。代码中可以找到部分尝试:

import matplotlib.pyplot as plt
from cycler import cycler

plt.rc("axes", prop_cycle=cycler("color", ["darkgray", "gray", "lightgray"])
)
plt.rcParams["axes.edgecolor"] = "0.15"
plt.rcParams["axes.linewidth"]  = 1.25

labels = ["lab1", "lab2"]

sizes = [2000, 3000]
def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        s =  '{p:.2f}%({v:d}%)'.format(p=pct,v=val)
        s = f"${val}_{{\\ {pct:.2f}\%}}$"
        return s
    return my_autopct


fig, ax = plt.subplots(figsize=(10, 3))

ax.pie(sizes, explode=(0,0.02), labels=labels, autopct=make_autopct(sizes))
ax.set_title("title")
ax.patch.set_edgecolor('black')  
ax.patch.set_linewidth('1')  
plt.savefig("title.png")

enter image description here

最佳答案

如果我理解您的问题,可能的解决方案如下:

# pip install matplotlib

import matplotlib.pyplot as plt
import numpy as np

# set chart style
plt.style.use('_mpl-gallery-nogrid')

# set data
x = [5, 2, 3, 4]

# set colors of segments
colors = plt.get_cmap('GnBu')(np.linspace(0.2, 0.7, len(x)))

# plot
fig, ax = plt.subplots()
ax.pie(x, colors=colors, radius=2, 
       wedgeprops={"linewidth": 2, "edgecolor": "black", 'antialiased': True}, # << HERE
       frame=False, startangle=0, autopct='%.1f%%', pctdistance=0.6)

plt.show()

enter image description here

关于python - 在 matplotlib 中绘制饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71817949/

相关文章:

python - django1.8-连接Excel中的字段并上传到数据库中

python - spyder matplotlib UserWarning : This call to matplotlib. use() 无效,因为已经选择了后端

后台 Python Matplotlib 更新图

python - 如何在 gui 中放置 python gglot 图?

python - matplotlib 颜色条和具有共享轴的直方图

python - 如何在类外使用方法对象(在类中)?

python - 在子命令后允许 argparse 全局标志

python - 将Python列表元素分成两部分合并

python - 如何仅用 3 个已知点制作连续的抛物线弧?

python - Matplotlib 忽略时区