gpt4 book ai didi

python - 无法在我的 matplotlib 饼图上设置背景

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:18 24 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 在 django 中制作服务器生成的饼图。我的示例 View 方法如下所示:

def test_plot(response):
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

num_signed_off = random.randint(0, 10)
num_reviewed = random.randint(0, 50)
num_unreviewed = random.randint(0, 50)

fig = Figure()
ax = fig.add_subplot(111, aspect='equal')
ax.pie([num_signed_off, num_reviewed, num_unreviewed],
labels=['Signed Off', 'Reviewed', 'Unreviewed'],
colors=['b', 'r', 'g'],
)
ax.set_title('My Overall Stats')
canvas=FigureCanvas(fig)
response=HttpResponse(content_type="image/png")
canvas.print_png(response)
return response

一切都很好,除了饼图的背景是丑陋的泥灰色。我希望它通过透明或具有白色背景来匹配它嵌入的页面的其余部分。我在 ax.pie 中找不到任何可以设置背景颜色或透明度的选项,并尝试在 fig.add_subplotax.set_axis_bgcolor 中设置“axis_bg_color” > 对输出没有任何影响。有什么方法可以修复这种背景颜色吗?

最佳答案

只需在图中添加一个facecolor参数即可:

fig = Figure(facecolor='white')

关于python - 无法在我的 matplotlib 饼图上设置背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240734/

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