Paul Tomblin
Paul Tomblin

Reputation: 182802

Can't set the background on my matplotlib pie chart

I'm trying to make a server-generated pie chart in django using matplotlib. My example view method looks like this:

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

Everything comes out great, except the background of the pie chart is an ugly muddy grey. I want it to match the rest of the page that it's embedded on, either by being transparent or by having a white background. I can't find any option in ax.pie that will set the background color or transparency, and attempts to set the "axis_bg_color" in either fig.add_subplot or ax.set_axis_bgcolor have not had any effect on the output. Is there some way I can fix this background color?

Upvotes: 2

Views: 3147

Answers (2)

Viktor Kerkez
Viktor Kerkez

Reputation: 46616

Just add a facecolor parameter to the Figure:

fig = Figure(facecolor='white')

Upvotes: 4

Alasdair
Alasdair

Reputation: 308999

This answer suggests you need to set axis_bg, not axis_bg_color as you have in your question.

Upvotes: 0

Related Questions