python matplotlib绘制等高线,plt.contour(),ax3.contour()和plt.contourf(),ax3.contour(), 同名函数
生活随笔
收集整理的這篇文章主要介紹了
python matplotlib绘制等高线,plt.contour(),ax3.contour()和plt.contourf(),ax3.contour(), 同名函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引用文章
https://blog.csdn.net/lanchunhui/article/details/70495353
-
首先這是由不同對象調用的函數,ax3指3D Figure對象即<class ‘mpl_toolkits.mplot3d.axes3d.Axes3D’>, plt指<class ‘module’>對象, 即matplotlib.pyplot, 是一個模塊, plt的函數(方法)就是這個模塊的函數(方法). 但它們ax3和plt卻具有相同名稱的函數(方法), 特別容易混淆, 真令人頭疼!!!
-
下面直接看官方doc解釋:
ax3.contour():
Signature: ax3.contour(X,Y,Z,*args,extend3d=False,stride=5,zdir='z',offset=None,**kwargs, ) Docstring: Create a 3D contour plot.Parameters ---------- X, Y, Z : array-likesInput data. extend3d : boolWhether to extend contour in 3D; defaults to False. stride : intStep size for extending contour. zdir : {'x', 'y', 'z'}The direction to use; defaults to 'z'. offset : scalarIf specified, plot a projection of the contour lines at thisposition in a plane normal to zdir *args, **kwargsOther arguments are forwarded to `matplotlib.axes.Axes.contour`.Returns ------- matplotlib.contour.QuadContourSet File: c:\users\huawei\appdata\local\programs\python\python36\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py Type: methodplt.contour():
Signature: plt.contour(*args, data=None, **kwargs) Docstring: Plot contours.Call signature::contour([X, Y,] Z, [levels], **kwargs)`.contour` and `.contourf` draw contour lines and filled contours, respectively. Except as noted, function signatures and return values are the same for both versions.Parameters ---------- X, Y : array-like, optionalThe coordinates of the values in *Z*.*X* and *Y* must both be 2-D with the same shape as *Z* (e.g.created via `numpy.meshgrid`), or they must both be 1-D suchthat ``len(X) == M`` is the number of columns in *Z* and``len(Y) == N`` is the number of rows in *Z*.If not given, they are assumed to be integer indices, i.e.``X = range(M)``, ``Y = range(N)``.Z : array-like(N, M)The height values over which the contour is drawn.levels : int or array-like, optionalDetermines the number and positions of the contour lines / regions.If an int *n*, use *n* data intervals; i.e. draw *n+1* contourlines. The level heights are automatically chosen.If array-like, draw contour lines at the specified levels.The values must be in increasing order.Returns ------- c : `~.contour.QuadContourSet`Other Parameters ---------------- corner_mask : bool, optionalEnable/disable corner masking, which only has an effect if *Z* isa masked array. If ``False``, any quad touching a masked point ismasked out. If ``True``, only the triangular corners of quadsnearest those points are always masked out, other triangularcorners comprising three unmasked points are contoured as usual.Defaults to :rc:`contour.corner_mask`, which defaults to ``True``.colors : color string or sequence of colors, optionalThe colors of the levels, i.e. the lines for `.contour` and theareas for `.contourf`.The sequence is cycled for the levels in ascending order. If thesequence is shorter than the number of levels, it's repeated.As a shortcut, single color strings may be used in place ofone-element lists, i.e. ``'red'`` instead of ``['red']`` to colorall levels with the same color. This shortcut does only work forcolor strings, not for other ways of specifying colors.By default (value *None*), the colormap specified by *cmap*will be used.alpha : float, optionalThe alpha blending value, between 0 (transparent) and 1 (opaque).cmap : str or `.Colormap`, optionalA `.Colormap` instance or registered colormap name. The colormapmaps the level values to colors.Defaults to :rc:`image.cmap`.If given, *colors* take precedence over *cmap*.norm : `~matplotlib.colors.Normalize`, optionalIf a colormap is used, the `.Normalize` instance scales the levelvalues to the canonical colormap range [0, 1] for mapping tocolors. If not given, the default linear scaling is used.vmin, vmax : float, optionalIf not *None*, either or both of these values will be supplied tothe `.Normalize` instance, overriding the default color scalingbased on *levels*.origin : {*None*, 'upper', 'lower', 'image'}, optionalDetermines the orientation and exact position of *Z* by specifyingthe position of ``Z[0, 0]``. This is only relevant, if *X*, *Y*are not given.- *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 leftcorner.- 'image': Use the value from :rc:`image.origin`.extent : (x0, x1, y0, y1), optionalIf *origin* is not *None*, then *extent* is interpreted as in`.imshow`: it gives the outer pixel boundaries. In this case, theposition of Z[0,0] is the center of the pixel, not a corner. If*origin* is *None*, then (*x0*, *y0*) is the position of Z[0,0],and (*x1*, *y1*) is the position of Z[-1,-1].This argument is ignored if *X* and *Y* are specified in the callto contour.locator : ticker.Locator subclass, optionalThe locator is used to determine the contour levels if theyare not given explicitly via *levels*.Defaults to `~.ticker.MaxNLocator`.extend : {'neither', 'both', 'min', 'max'}, optional, default: 'neither'Determines the ``contourf``-coloring of values that are outside the*levels* range.If 'neither', values outside the *levels* range are not colored.If 'min', 'max' or 'both', color the values below, above or belowand above the *levels* range.Values below ``min(levels)`` and above ``max(levels)`` are mappedto the under/over values of the `.Colormap`. Note, that mostcolormaps do not have dedicated colors for these by default, sothat the over and under values are the edge values of the colormap.You may want to set these values explicitly using`.Colormap.set_under` and `.Colormap.set_over`... note::An exising `.QuadContourSet` does not get notified ifproperties of its colormap are changed. Therefore, an explicitcall `.QuadContourSet.changed()` is needed after modifying thecolormap. The explicit call can be left out, if a colorbar isassigned to the `.QuadContourSet` because it internally calls`.QuadContourSet.changed()`.Example::x = np.arange(1, 10)y = x.reshape(-1, 1)h = x * ycs = plt.contourf(h, levels=[10, 30, 50],colors=['#808080', '#A0A0A0', '#C0C0C0'], extend='both')cs.cmap.set_over('red')cs.cmap.set_under('blue')cs.changed()xunits, yunits : registered units, optionalOverride axis units by specifying an instance of a:class:`matplotlib.units.ConversionInterface`.antialiased : bool, optionalEnable antialiasing, overriding the defaults. Forfilled contours, the default is *True*. For line contours,it is taken from :rc:`lines.antialiased`.Nchunk : int >= 0, optionalIf 0, no subdivision of the domain. Specify a positive integer todivide the domain into subdomains of *nchunk* by *nchunk* quads.Chunking reduces the maximum length of polygons generated by thecontouring algorithm which reduces the rendering workload passedon to the backend and also requires slightly less RAM. It canhowever introduce rendering artifacts at chunk boundaries dependingon the backend, the *antialiased* flag and value of *alpha*.linewidths : float or sequence of float, optional*Only applies to* `.contour`.The line width of the contour lines.If a number, all levels will be plotted with this linewidth.If a sequence, the levels in ascending order will be plotted withthe linewidths in the order specified.Defaults to :rc:`lines.linewidth`.linestyles : {*None*, 'solid', 'dashed', 'dashdot', 'dotted'}, optional*Only applies to* `.contour`.If *linestyles* is *None*, the default is 'solid' unless the linesare monochrome. In that case, negative contours will take theirlinestyle from :rc:`contour.negative_linestyle` setting.*linestyles* can also be an iterable of the above stringsspecifying a set of linestyles to be used. If thisiterable is shorter than the number of contour levelsit will be repeated as necessary.hatches : List[str], optional*Only applies to* `.contourf`.A list of cross hatch patterns to use on the filled areas.If None, no hatching will be added to the contour.Hatching is supported in the PostScript, PDF, SVG and Aggbackends only.Notes ----- 1. `.contourf` differs from the MATLAB version in that it does not drawthe polygon edges. To draw edges, add line contours with calls to`.contour`.2. `.contourf` fills intervals that are closed at the top; that is, forboundaries *z1* and *z2*, the filled region is::z1 < Z <= z2except for the lowest interval, which is closed on both sides (i.e.it includes the lowest value). File: c:\users\huawei\appdata\local\programs\python\python36\lib\site-packages\matplotlib\pyplot.py Type: functionax3.contourf():
Signature: ax3.contourf(X, Y, Z, *args, zdir='z', offset=None, **kwargs) Docstring: Create a 3D filled contour plot.Parameters ---------- X, Y, Z : array-likesInput data. zdir : {'x', 'y', 'z'}The direction to use; defaults to 'z'. offset : scalarIf specified, plot a projection of the contour lines at thisposition in a plane normal to zdir *args, **kwargsOther arguments are forwarded to `matplotlib.axes.Axes.contourf`.Returns ------- matplotlib.contour.QuadContourSetNotes ----- .. versionadded:: 1.1.0The *zdir* and *offset* parameters. File: c:\users\huawei\appdata\local\programs\python\python36\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py Type: methodplt.contourf():
Signature: plt.contourf(*args, data=None, **kwargs) Docstring: Plot contours.Call signature::contour([X, Y,] Z, [levels], **kwargs)`.contour` and `.contourf` draw contour lines and filled contours, respectively. Except as noted, function signatures and return values are the same for both versions.Parameters ---------- X, Y : array-like, optionalThe coordinates of the values in *Z*.*X* and *Y* must both be 2-D with the same shape as *Z* (e.g.created via `numpy.meshgrid`), or they must both be 1-D suchthat ``len(X) == M`` is the number of columns in *Z* and``len(Y) == N`` is the number of rows in *Z*.If not given, they are assumed to be integer indices, i.e.``X = range(M)``, ``Y = range(N)``.Z : array-like(N, M)The height values over which the contour is drawn.levels : int or array-like, optionalDetermines the number and positions of the contour lines / regions.If an int *n*, use *n* data intervals; i.e. draw *n+1* contourlines. The level heights are automatically chosen.If array-like, draw contour lines at the specified levels.The values must be in increasing order.Returns ------- c : `~.contour.QuadContourSet`Other Parameters ---------------- corner_mask : bool, optionalEnable/disable corner masking, which only has an effect if *Z* isa masked array. If ``False``, any quad touching a masked point ismasked out. If ``True``, only the triangular corners of quadsnearest those points are always masked out, other triangularcorners comprising three unmasked points are contoured as usual.Defaults to :rc:`contour.corner_mask`, which defaults to ``True``.colors : color string or sequence of colors, optionalThe colors of the levels, i.e. the lines for `.contour` and theareas for `.contourf`.The sequence is cycled for the levels in ascending order. If thesequence is shorter than the number of levels, it's repeated.As a shortcut, single color strings may be used in place ofone-element lists, i.e. ``'red'`` instead of ``['red']`` to colorall levels with the same color. This shortcut does only work forcolor strings, not for other ways of specifying colors.By default (value *None*), the colormap specified by *cmap*will be used.alpha : float, optionalThe alpha blending value, between 0 (transparent) and 1 (opaque).cmap : str or `.Colormap`, optionalA `.Colormap` instance or registered colormap name. The colormapmaps the level values to colors.Defaults to :rc:`image.cmap`.If given, *colors* take precedence over *cmap*.norm : `~matplotlib.colors.Normalize`, optionalIf a colormap is used, the `.Normalize` instance scales the levelvalues to the canonical colormap range [0, 1] for mapping tocolors. If not given, the default linear scaling is used.vmin, vmax : float, optionalIf not *None*, either or both of these values will be supplied tothe `.Normalize` instance, overriding the default color scalingbased on *levels*.origin : {*None*, 'upper', 'lower', 'image'}, optionalDetermines the orientation and exact position of *Z* by specifyingthe position of ``Z[0, 0]``. This is only relevant, if *X*, *Y*are not given.- *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 leftcorner.- 'image': Use the value from :rc:`image.origin`.extent : (x0, x1, y0, y1), optionalIf *origin* is not *None*, then *extent* is interpreted as in`.imshow`: it gives the outer pixel boundaries. In this case, theposition of Z[0,0] is the center of the pixel, not a corner. If*origin* is *None*, then (*x0*, *y0*) is the position of Z[0,0],and (*x1*, *y1*) is the position of Z[-1,-1].This argument is ignored if *X* and *Y* are specified in the callto contour.locator : ticker.Locator subclass, optionalThe locator is used to determine the contour levels if theyare not given explicitly via *levels*.Defaults to `~.ticker.MaxNLocator`.extend : {'neither', 'both', 'min', 'max'}, optional, default: 'neither'Determines the ``contourf``-coloring of values that are outside the*levels* range.If 'neither', values outside the *levels* range are not colored.If 'min', 'max' or 'both', color the values below, above or belowand above the *levels* range.Values below ``min(levels)`` and above ``max(levels)`` are mappedto the under/over values of the `.Colormap`. Note, that mostcolormaps do not have dedicated colors for these by default, sothat the over and under values are the edge values of the colormap.You may want to set these values explicitly using`.Colormap.set_under` and `.Colormap.set_over`... note::An exising `.QuadContourSet` does not get notified ifproperties of its colormap are changed. Therefore, an explicitcall `.QuadContourSet.changed()` is needed after modifying thecolormap. The explicit call can be left out, if a colorbar isassigned to the `.QuadContourSet` because it internally calls`.QuadContourSet.changed()`.Example::x = np.arange(1, 10)y = x.reshape(-1, 1)h = x * ycs = plt.contourf(h, levels=[10, 30, 50],colors=['#808080', '#A0A0A0', '#C0C0C0'], extend='both')cs.cmap.set_over('red')cs.cmap.set_under('blue')cs.changed()xunits, yunits : registered units, optionalOverride axis units by specifying an instance of a:class:`matplotlib.units.ConversionInterface`.antialiased : bool, optionalEnable antialiasing, overriding the defaults. Forfilled contours, the default is *True*. For line contours,it is taken from :rc:`lines.antialiased`.Nchunk : int >= 0, optionalIf 0, no subdivision of the domain. Specify a positive integer todivide the domain into subdomains of *nchunk* by *nchunk* quads.Chunking reduces the maximum length of polygons generated by thecontouring algorithm which reduces the rendering workload passedon to the backend and also requires slightly less RAM. It canhowever introduce rendering artifacts at chunk boundaries dependingon the backend, the *antialiased* flag and value of *alpha*.linewidths : float or sequence of float, optional*Only applies to* `.contour`.The line width of the contour lines.If a number, all levels will be plotted with this linewidth.If a sequence, the levels in ascending order will be plotted withthe linewidths in the order specified.Defaults to :rc:`lines.linewidth`.linestyles : {*None*, 'solid', 'dashed', 'dashdot', 'dotted'}, optional*Only applies to* `.contour`.If *linestyles* is *None*, the default is 'solid' unless the linesare monochrome. In that case, negative contours will take theirlinestyle from :rc:`contour.negative_linestyle` setting.*linestyles* can also be an iterable of the above stringsspecifying a set of linestyles to be used. If thisiterable is shorter than the number of contour levelsit will be repeated as necessary.hatches : List[str], optional*Only applies to* `.contourf`.A list of cross hatch patterns to use on the filled areas.If None, no hatching will be added to the contour.Hatching is supported in the PostScript, PDF, SVG and Aggbackends only.Notes ----- 1. `.contourf` differs from the MATLAB version in that it does not drawthe polygon edges. To draw edges, add line contours with calls to`.contour`.2. `.contourf` fills intervals that are closed at the top; that is, forboundaries *z1* and *z2*, the filled region is::z1 < Z <= z2except for the lowest interval, which is closed on both sides (i.e.it includes the lowest value). File: c:\users\huawei\appdata\local\programs\python\python36\lib\site-packages\matplotlib\pyplot.py Type: function總結
以上是生活随笔為你收集整理的python matplotlib绘制等高线,plt.contour(),ax3.contour()和plt.contourf(),ax3.contour(), 同名函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: i.e.、e.g.、etc.都是什么英文
- 下一篇: python matplotlib二维平