gpt4 book ai didi

python - 在 matplotlib 中绘制饼图

转载 作者:行者123 更新时间:2023-12-05 05:43:29 26 4
gpt4 key购买 nike

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

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/

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