我正在尝试使用 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_subplot
或ax.set_axis_bgcolor
对输出没有任何影响。有什么办法可以修复这个背景颜色吗?