如何将轴标签添加到 python 中的 imshow 图?
How to add axis labels to imshow plots in python?
我从 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' object has no attribute 'ylabel'
除此之外,我只找到how to remove the axis labels,但没有找到如何添加它们。
额外问题:如何让刻度范围从 -3
到 3
而不是从 0
到 60
?
要指定轴标签:
matplotlib.pyplot.xlabel
x
轴
matplotlib.pyplot.ylabel
用于 y
轴
关于您的奖励问题,请考虑 extent
kwarg。 (感谢@Jona)。
此外,考虑 PEP 8 -- Style Guide for Python Code 推荐的绝对导入:
Absolute imports are recommended, as they are usually more readable
and tend to be better behaved (or at least give better error messages)
if the import system is incorrectly configured (such as when a
directory inside a package ends up on sys.path)
import matplotlib.pyplot as plt
import numpy as np
# the function that I'm going to plot
def z_func(x,y):
return (x+y**2)
x = np.arange(-3.0,3.0,0.1)
y = np.arange(-3.0,3.0,0.1)
X,Y = np.meshgrid(x, y) # grid of point
Z = z_func(X, Y) # evaluation of the function on the grid
plt.xlabel('x axis')
plt.ylabel('y axis')
im = plt.imshow(Z,cmap=plt.cm.RdBu, extent=[-3, 3, -3, 3]) # drawing the function
plt.colorbar(im) # adding the colobar on the right
plt.show()
你得到:
我从 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' object has no attribute 'ylabel'
除此之外,我只找到how to remove the axis labels,但没有找到如何添加它们。
额外问题:如何让刻度范围从 -3
到 3
而不是从 0
到 60
?
要指定轴标签:
matplotlib.pyplot.xlabel
x
轴matplotlib.pyplot.ylabel
用于y
轴
关于您的奖励问题,请考虑 extent
kwarg。 (感谢@Jona)。
此外,考虑 PEP 8 -- Style Guide for Python Code 推荐的绝对导入:
Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path)
import matplotlib.pyplot as plt
import numpy as np
# the function that I'm going to plot
def z_func(x,y):
return (x+y**2)
x = np.arange(-3.0,3.0,0.1)
y = np.arange(-3.0,3.0,0.1)
X,Y = np.meshgrid(x, y) # grid of point
Z = z_func(X, Y) # evaluation of the function on the grid
plt.xlabel('x axis')
plt.ylabel('y axis')
im = plt.imshow(Z,cmap=plt.cm.RdBu, extent=[-3, 3, -3, 3]) # drawing the function
plt.colorbar(im) # adding the colobar on the right
plt.show()
你得到: