2

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

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 中找不到任何设置背景颜色或透明度的选项,并尝试在其中设置“axis_bg_color”fig.add_subplotax.set_axis_bgcolor对输出没有任何影响。有什么办法可以修复这个背景颜色吗?

4

2 回答 2

4

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

fig = Figure(facecolor='white')
于 2013-08-14T19:45:38.000 回答
0

这个答案表明你需要设置axis_bg,而不是axis_bg_color你在你的问题中。

于 2013-08-14T19:45:23.510 回答