之前我在使用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?
ipython --pylab
命令会自动导入from matploblib.pyplot import *
。 - Andy Hayden