Python:带有imshow的2D彩色地图 [英] Python: 2D color map with imshow

查看:869
本文介绍了Python:带有imshow的2D彩色地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用颜色表示二维图上两个变量的函数。
我遇到过这个例子在这里

  from numpy import exp,arange 
from pylab import meshgrid,cm,imshow,轮廓,clabel,colorbar,轴,标题,显示
#我要绘制的函数
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)点的网格
Z = z_func(X,Y)#网格上函数的求值

im = imshow(Z,cmap = cm.RdBu)#绘制函数
#将轮廓线与标签相加
cset = contour(Z,arange(-1,1.5,0.2), linewidth = 2,cmap = cm.Set2)
clabel(cset,inline = True,fmt ='%1.1f',fontsize = 10)
colorbar(im)#将colobar添加到右边
#乳胶时尚标题
title('$ z =(1-x ^ 2 + y ^ 3)e ^ { - (x ^ 2 + y ^ 2)/ 2} $')
show()

whi ch产生

然而,轴的比例和极限与实际的x和y数据(两者都在-3和3之间)不一致。如何使它们对应于实际的数据?

解决方案

添加 extent =( - 3,3, 3,-3)调用 imshow
extent =( - 3,3, - 3,3)(注意标志中不幸的变化!)调用 contour

  import numpy as np 
import matplotlib.pyplot as plt


def z_func(x,y):
return(1 - (x ** 2 + y ** 3))* np.exp( - (x ** 2 + y ** 2)/ 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)
Z = z_func(X, Y)


im = plt.imshow(Z,cmap = plt.cm.RdBu,extent =( - 3,3,3,-3))
cset = plt.contour(Z,np.arange(-1,1.5,0.2),linewidths = 2,
cmap = plt.cm.Set2,
extent =( - 3,3,-3,3 ))
plt.clabel(cset,inline = True,fmt ='%1.1f',fontsize = 10)
plt.colorbar(im)

plt.title( '$ z =(1-x ^ 2 + y ^ 3)e ^ { - (x ^ 2 + y ^ 2) / 2} $')

plt.show()


I am trying to represent a function of two variable on a two dimensional graph using color. I came across this example 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()

which produces

However, the axis scales and limits do not correspond to the real x and y data(both of which are between -3 and 3). How to make them correspond to the actual data?

解决方案

Add extent=(-3, 3, 3, -3) to the call to imshow and extent=(-3, 3, -3, 3) (note the unfortunately tricky change in signs!) to the call to contour:

import numpy as np
import matplotlib.pyplot as plt


def z_func(x, y):
    return (1 - (x ** 2 + y ** 3)) * np.exp(-(x ** 2 + y ** 2) / 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)
Z = z_func(X, Y) 


im = plt.imshow(Z, cmap=plt.cm.RdBu, extent=(-3, 3, 3, -3))  
cset = plt.contour(Z, np.arange(-1, 1.5, 0.2), linewidths=2,
                   cmap=plt.cm.Set2,
                   extent=(-3, 3, -3, 3))
plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
plt.colorbar(im)  

plt.title('$z=(1-x^2+y^3) e^{-(x^2+y^2)/2}$')

plt.show()

这篇关于Python:带有imshow的2D彩色地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆