【发布时间】:2014-07-22 20:56:22
【问题描述】:
我正在尝试使用颜色在二维图上表示两个变量的函数。 我遇到了这个例子here:
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
# the function that I'm going to plot
def z_func(x,y):
return (1-(x**2+y**3))*exp(-(x**2+y**2)/2)
x = arange(-3.0,3.0,0.1)
y = arange(-3.0,3.0,0.1)
X,Y = meshgrid(x, y) # grid of point
Z = z_func(X, Y) # evaluation of the function on the grid
im = imshow(Z,cmap=cm.RdBu) # drawing the function
# adding the Contour lines with labels
cset = contour(Z,arange(-1,1.5,0.2),linewidths=2,cmap=cm.Set2)
clabel(cset,inline=True,fmt='%1.1f',fontsize=10)
colorbar(im) # adding the colobar on the right
# latex fashion title
title('$z=(1-x^2+y^3) e^{-(x^2+y^2)/2}$')
show()
产生
但是,轴的刻度和范围与实际的 x 和 y 数据不对应(两者都在 -3 和 3 之间)。如何让它们与实际数据对应?
【问题讨论】:
-
在 matplotlib
imshow文档和 StackOverflow 上搜索有关imshow的extent参数的信息。例如,stackoverflow.com/questions/6999621/… 和 matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow -
您还需要将
extent参数添加到对contour的调用中。 -
一个小错误:标题与正在绘制的函数不对应。正确的版本应该是
title('$z=(1-(x^2+y^3)) e^{-(x^2+y^2)/2}$')
标签: python graphics graph matplotlib