Contour in Python

限于喜欢 提交于 2021-01-28 08:01:00

问题


How Z is calcutalted in

from matplotlib.pyplot import contour

contour([X, Y,] Z, [levels], **kwargs)

to draw a contour?

I know that Z means: The height values over which the contour is drawn.

But is it drawn by calculating a standard deviation or something like that?

An average between each point I have?


回答1:


Z represents a quantity dependent on both X and Y axes. If X and Y represent a plane, Z can be thought of as a surface, whose point height depends on the X and Y coordinates of that given point. The contour is a "top view" of that surface, a projection. An example are the contour lines which report the heights of the mountains (Z) as longitude (X) and latitude (Y) change.
The contour function of matplotlib, as you wrote it, plots the values expressed in the Z variable (two-dimensional numpy.ndarray, as X and Y) as they are, without further processing. The relationship between Z and X and Y is defined outside the plot function.
I report an example below which, perhaps it may be useful:

# IMPORT
import numpy as np
import matplotlib.pyplot as pl

# INPUT
N = 100
x_min = 0
x_max = 10
y_min = 0
y_max = 10
z_min = 0
z_max = 50
z_step = 1
red = '#de7677'

# DEFINE MESH GRID
x = np.linspace(x_min, x_max, N)
y = np.linspace(y_min, y_max, N)
XX, YY = np.meshgrid(x, y)

# CALCULATE ZZ AS A FUNCTION OF XX AND YY, FOR ESAMPLE, THEIR SUM
ZZ = YY + XX

# PLOT THE CONTOUR
fig, ax = pl.subplots(figsize = (10, 10))

cont = ax.contour(XX,
                  YY,
                  ZZ,
                  levels = np.arange(z_min, z_max + z_step, z_step),
                  colors = red)

# SET THE CONTOUR LABELS
pl.clabel(cont, fmt = '%d')

# SET THE X AND Y LABEL
ax.set_xlabel('X')
ax.set_ylabel('Y')

pl.show()


来源:https://stackoverflow.com/questions/62123497/contour-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!