【发布时间】:2019-05-09 14:59:24
【问题描述】:
我从this website 复制(并简化)以下代码,使用imshow 绘制具有两个变量的函数的结果。
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 (x+y**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
colorbar(im) # adding the colobar on the right
show()
如何在绘图中添加轴标签(如 'x' 和 'y' 或 'var1 和 'var2')?在 R 中,我会在(大部分)绘图函数中使用xlab = 'x'。
我尝试了im.ylabel('y')
AttributeError: 'AxesImage' 对象没有属性 'ylabel'
除此之外,我只找到how to remove the axis labels,但没有找到如何添加它们。
额外问题:如何让刻度范围从-3 到3 而不是从0 到60?
【问题讨论】:
-
也许你可以试试:
set_xlabel()和set_ylabel()matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlabel.html matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_ylabel.html -
我不认为
pylab被推荐了,应该使用pyplot- 见this Q/A。如果您坚持使用 pylab,您可以随时将ylabel和xlabel添加到您的导入列表中,from pylab import ylabel;ylabel("y axis") -
@Jona
im.set_xlabel('x')给出了类似的错误:AttributeError: 'AxesImage' object has no attribute 'set_xlabel'
标签: python numpy matplotlib data-visualization axis-labels