【发布时间】:2016-10-13 14:28:02
【问题描述】:
我正在使用以下代码生成一些等高线图,
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
import numpy as np
from numpy import exp,arange
import matplotlib.pyplot as plt
def z_func(x,y):
func = 3.0*(1.0 - x)**2*np.exp(-x**2 - (y+1.0)**2) - 10.0*(x/5.0 - x**3 - y**5)*np.exp(-x**2 - y**2) - 0.33*np.exp(-(x + 1.0)**2 - y**2)
return func
x = arange(-4.0,4.0,0.1)
y = arange(-4.0,4.0,0.1)
X,Y = meshgrid(x, y) # grid of point
Z = z_func(X, Y)
fig = plt.figure(figsize=(10,6))
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('peaks function')
show()
我从 StackExchange 上的某个地方偷了它。我很难让 x 和 y 轴显示正确的域 [-4,4]。已经发布了许多对我不起作用的解决方案,例如Change values on matplotlib imshow() graph axis 和correcting the axes using imshow,但既不能保持图像原样,也不能重新标记轴。救命!!!
【问题讨论】:
-
此代码无效。您只想为 imshow 设置轴限制吗?
-
代码对我有用。我忘记了我的进口......让我现在输入。我希望轴反映我的输入变量的实际界限 [-4,4]
标签: python matplotlib imshow