【问题标题】:Can't set the background on my matplotlib pie chart无法在我的 matplotlib 饼图上设置背景
【发布时间】:2013-08-15 03:45:38
【问题描述】:

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

【问题讨论】:

    标签: python django matplotlib


    【解决方案1】:

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

    fig = Figure(facecolor='white')
    

    【讨论】:

      【解决方案2】:

      This answer 建议您需要设置axis_bg,而不是您在问题中的axis_bg_color

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        • 2015-09-22
        相关资源
        最近更新 更多