Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3:绘图f(x,y),最好使用matplotlib_Python_Variables_Matplotlib - Fatal编程技术网

Python3:绘图f(x,y),最好使用matplotlib

Python3:绘图f(x,y),最好使用matplotlib,python,variables,matplotlib,Python,Variables,Matplotlib,是否有一种方法,最好使用matplotlib,在python中绘制一个2变量函数f(x,y); 提前谢谢。如果您有Z的expresson 如果您有一个Z表达式,则可以生成网格,并调用surface\u plot: #!/usr/bin/python3 import sys import matplotlib import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator from matplotlib imp

是否有一种方法,最好使用matplotlib,在python中绘制一个2变量函数f(x,y); 提前谢谢。

如果您有Z的expresson 如果您有一个
Z
表达式,则可以生成网格,并调用
surface\u plot

#!/usr/bin/python3

import sys

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

import numpy
from numpy.random import randn, shuffle
from scipy import linspace, meshgrid, arange, empty, concatenate, newaxis, shape


# =========================
## generating ordered data:

N = 32
x = sorted(randn(N))
y = sorted(randn(N))

X, Y = meshgrid(x, y)
Z = X**2 + Y**2


# ======================================
## reference picture (X, Y and Z in 2D):

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)

title = ax.set_title("plot_surface: given X, Y and Z as 2D:")
title.set_y(1.01)

ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))

fig.tight_layout()
fig.savefig('3D-constructing-{}.png'.format(N))
结果:

如果你没有Z的表达式
surface\u plot
仅用于上述功能。这是不可能的,如果没有Z的表达式,而只是有一个存储在列表列表中的数据:
[[x1,y1,z1],[x2,y2,z2],…]
。在这种情况下,您可以使用

在下面的代码中,我构建了X、Y和Z的2D,然后重新塑造数据,使其具有1D中的X、Y和Z,将其洗牌,并使用
plot_trisurf
绘制相同的数据:

#!/usr/bin/python3

import sys

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

import numpy
from numpy.random import randn, shuffle
from scipy import linspace, meshgrid, arange, empty, concatenate, newaxis, shape


# =========================
## generating ordered data:

N = 128
x = sorted(randn(N))
y = sorted(randn(N))

X, Y = meshgrid(x, y)
Z = X**2 + Y**2


# =======================
## re-shaping data in 1D:

# flat and prepare for concat:
X_flat = X.flatten()[:, newaxis]
Y_flat = Y.flatten()[:, newaxis]
Z_flat = Z.flatten()[:, newaxis]

DATA = concatenate((X_flat, Y_flat, Z_flat), axis=1)

shuffle(DATA)

Xs = DATA[:,0]
Ys = DATA[:,1]
Zs = DATA[:,2]


# ====================================================
## plotting surface using X, Y and Z given as 1D data:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)

title = ax.set_title("plot_trisurf: takes X, Y and Z as 1D")
title.set_y(1.01)

ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))

fig.tight_layout()
fig.savefig('3D-reconstructing-{}.png'.format(N))
结果:


你们速度很快。谢谢!