Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

python - Python3:Plot f(x,y), preferably using matplotlib

Is there a way, preferably using matplotlib, to plot a 2-variable function f(x,y) in python; Thank you, in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If You have an expresson for Z

If You have an expression for Z You can generate mesh, and call for surface_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))

Result:

enter image description here

If You don't have an expression for Z

surface_plot function used above only accepts X, Y and Z as 2D arrays. Which is not possible if don't have an expression for Z -- but just have a data stored in a list of lists: [[x1, y1, z1],[x2,y2,z2],...]. In this case You can use plot_trisurf.

In a code below I construct 2D of X, Y and Z, then re-shape the data to have X, Y and Z in 1D, shuffle it, and use plot_trisurf to plot the same data:

#!/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))

Result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...