X, Y : array-like, optional #The coordinates of the values in *Z*. Z的坐标 Z : array-like(N, M) #The height values over which the contour is drawn. 绘制轮廓的高度
x,y,z 就是分别设置相应的位置参数,而且表示的维度类型必须匹配。
levles
1 2
levels : int or array-like, optional #Determines the number and positions of the contour lines / regions.
levels 是用来确定轮廓线/区域的数量和位置的。如果为 int(n) 则使用 n 个数据间隔,绘制 n+1 个等高线,水平高度是自动选择的。
部分其他参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
corner_mask : bool, optional #启用/禁用边角遮罩,默认True colors : color string or sequence of colors, optional #颜色 alpha : float, optional #透明度 cmap : str or `.Colormap`, optional #色彩图,colors 参数可覆盖效果 norm : `~matplotlib.colors.Normalize`, optional #缩放色阶值映射到规范颜色范围 vmin, vmax : float, optional #覆盖默认的颜色缩放比例 origin : {*None*, 'upper', 'lower', 'image'}, optional #确定 z 的方向和确切位置 # - *None*: ``Z[0, 0]`` is at X=0, Y=0 in the lower left corner. #- 'lower': ``Z[0, 0]`` is at X=0.5, Y=0.5 in the lower left corner. #- 'upper': ``Z[0, 0]`` is at X=N+0.5, Y=0.5 in the upper left corner. #- 'image': Use the value from :rc:`image.origin`. extend : {'neither', 'both', 'min', 'max'}, optional, default: 'neither'#确定轮廓线外的值的颜色 linewidths : float or sequence of float, optional #线宽 linestyles : {*None*, 'solid', 'dashed', 'dashdot', 'dotted'}, optional #线型
其实这些参数加上以后的效果好像并不是太明显,具体的用法后续在讨论。
上图的间隙之间还是有点大,我们可以使用 plt.contourf 函数来进行等高线的填充。
1 2 3 4 5 6 7 8 9 10 11 12
%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np deff(x,y): return np.sin(x)**10+np.cos(10+y*x)*np.cos(x) x = np.linspace(0,5,50) y = np.linspace(0,5,40) X,Y = np.meshgrid(x,y) Z = f(X,Y) plt.contourf(X,Y,Z,20,cmap='Blues') plt.colorbar() #显示颜色条
%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np deff(x,y): return np.sin(x)**10+np.cos(10+y*x)*np.cos(x) x = np.linspace(0,5,50) y = np.linspace(0,5,40) X,Y = np.meshgrid(x,y) Z = f(X,Y) contours = plt.contour(X,Y,Z,3,colors='black') plt.clabel(contours,inline=True,fontsize=8) plt.imshow(Z,extent=[0,5,0,5],origin='lower',cmap='Blues',alpha=0.5) plt.colorbar()
这样来看就清楚了很多,下面来就上面用到的函数进行参数的说明。
4.plt.clabel
1 2 3 4
#Signature: plt.clabel(CS, *args, **kwargs) #Docstring: #Label a contour plot. #便签轮廓图
主要参数解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cs : `.ContourSet` #The ContourSet to label. levels : array-like, optional #A list of level values, that should be labeled. The list must be #a subset of ``cs.levels``. If not given, all levels are labeled. fontsize : string or float, optional #Size in points or relative size e.g., 'smaller', 'x-large'. #See `.Text.set_size` for accepted string values. colors : color-spec, optional #The label colors: inline : bool, optional #If ``True`` the underlying contour is removed where the label is #placed. Default is ``True``. inline_spacing : float, optional #Space in pixels to leave on each side of label when #placing inline. Defaults to 5. fmt : string or dict, optional
plt.imshow(X,cmap=None,norm=None,aspect=None, interpolation=None,alpha=None,vmin=None,vmax=None, origin=None,extent=None,shape=<deprecated parameter>, filternorm=1,filterrad=4.0,imlim=<deprecated parameter>, resample=None,url=None,*,data=None,**kwargs,) Docstring: #Display an image, i.e. data on a 2D regular raster. #显示图像,二维常规光栅上的数据
对这个函数进行参数解析前,先来看一下,只使用该函数时画出的图是什么样子的。
1 2 3 4 5 6 7 8 9 10 11 12
%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np deff(x,y): return np.sin(x)**10+np.cos(10+y*x)*np.cos(x) x = np.linspace(0,5,50) y = np.linspace(0,5,40) X,Y = np.meshgrid(x,y) Z = f(X,Y) plt.imshow(Z,extent=[0,5,0,5],origin='lower',cmap='Blues') plt.colorbar()
可以看到图像有点被像素画了,这就是 imshow 函数的作用。
主要参数解析
1 2 3 4 5
X : array-like or PIL image #The image data. Supported array shapes are: - (M, N): an image with scalar data. The data is visualized - (M, N, 3): an image with RGB values (0-1 float or0-255 int). - (M, N, 4): an image with RGBA values (0-1 float or0-255 int),
第一个参数用来定义图像数据,支持三种形式。
-(M,N):具有标量数据的图像。数据是可视化的
-(M,N,3):具有RGB值(0-1浮点或0-255整数)的图像
-(M,N,4):具有RGBA值(0-1浮点或0-255 int)的图像
1 2 3 4 5 6 7 8 9 10 11
%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np x = np.random.randn(5) y = np.random.randn(5) print(x,y) plt.imshow((x,y),extent=[0,5,0,5],origin='lower',cmap='Blues') plt.colorbar() ##x [-0.49486504 -0.99105513 3.27752515 0.02882249 0.04733611] ##y [ 0.88204904 -0.72641683 0.95912824 -0.36288482 1.53353979]
1 2 3 4
aspect : {'equal', 'auto'} or float, optional #Controls the aspect ratio of the axes. #equal:确保宽高比为1 #auto:轴报错固定,方向调整为使数据符合坐标轴
用来控制坐标轴的横纵比。
1 2 3 4 5 6 7 8 9
%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np x = np.random.randn(5) y = np.random.randn(5) print(x,y) plt.imshow((x,y),extent=[0,5,0,5],origin='lower',cmap='Blues',aspect='0.1') plt.colorbar()
#spline36 %matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np x = np.random.randn(5) y = np.random.randn(5) print(x,y) plt.imshow((x,y),extent=[0,5,0,5],origin='lower',cmap='Blues',interpolation='spline36') plt.colorbar() ## x[-0.2397638 -0.89091552 0.60802011 2.20626021 -0.28450459] ## y[-1.78338935 1.26923305 0.49671364 -0.72569517 1.36340109]
1 2 3 4 5 6 7 8 9 10 11 12
#spline16 %matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np x = np.random.randn(5) y = np.random.randn(5) print(x,y) plt.imshow((x,y),extent=[0,5,0,5],origin='lower',cmap='Blues',interpolation='spline16') plt.colorbar() ## x[ 2.52507531 1.66726626 0.41991259 0.70137512 -0.55313458] ## y[ 0.27079771 -0.1188416 3.21609489 -0.11450694 -0.5017201 ]
其实我们可以从上两个示例中看出来,这个参数改变了他的像素模式,部分边缘进行了变化。
1 2 3 4 5 6 7 8
origin : {'upper', 'lower'}, optional #Place the [0,0] index of the array in the upper left or lower left #corner of the axes. The convention 'upper' is typically used for #matrices and images. #If not given, :rc:`image.origin` is used, defaulting to 'upper'. extent : scalars (left, right, bottom, top), optional #The bounding box in data coordinates that the image will fill. #The image is stretched individually along x and y to fill the box.