以前,我遇到了多个 Matplotlib 图形之间的干扰问题。最后,我发现了一个问题,即某些 pyplot 函数不附加到它们的图形实例,但可以在并行创建的其他一些图形实例中呈现。
这是一些示例代码:
from django.http import HttpResponse
from numpy import arange, meshgrid
from matplotlib.mlab import bivariate_normal
def show_chart(request):
delta = 0.025
x = arange(-3.0, 3.0, delta)
y = arange(-2.0, 2.0, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
from matplotlib.pyplot import figure, contour
fig1 = figure(figsize=(4, 4), facecolor='white')
contour(X, Y, Z)
response = HttpResponse(content_type='image/png')
fig1.savefig(response, format='png')
fig1.clear()
return response
上面示例中的轮廓 pyplot 函数可以在 fig1 中呈现,但有时也可以在其他并行生成的图中呈现。这很烦人。有什么方法可以将轮廓 pyplot 函数附加到 fig1?