我正在尝试使用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_subplot
或ax.set_axis_bgcolor
中设置“axis_bg_color”对输出没有任何影响。有什么方法可以修复这种背景颜色吗?
答案 0 :(得分:3)
只需在图中添加facecolor
参数:
fig = Figure(facecolor='white')
答案 1 :(得分:0)
This answer建议您在问题中设置axis_bg
,而不是axis_bg_color
。