Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use matplotlib to make a 3d plot given a z function

Tags:

I have a z function that accepts x and y parameters and returns a z output. I want to plot this in 3d and set the scales. How can I do this easily? I've spent way too much time looking through the documentation and not once do I see a way to do this.

like image 863
WhatsInAName Avatar asked Jan 04 '12 06:01

WhatsInAName


People also ask

How do you plot a 3d function in Python?

We could plot 3D surfaces in Python too, the function to plot the 3D surfaces is plot_surface(X,Y,Z), where X and Y are the output arrays from meshgrid, and Z=f(X,Y) or Z(i,j)=f(X(i,j),Y(i,j)). The most common surface plotting functions are surf and contour. TRY IT!

Can Matplotlib Pyplot be used to display 3d plots?

Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit.

How do I draw a 3d shape in Matplotlib?

The 3d plot is enabled by importing the mplot3d toolkit., which comes with your standard Matplotlib. After importing, 3D plots can be created by passing the keyword projection=”3d” to any of the regular axes creation functions in Matplotlib.

What Matplotlib function can be used to draw a surface plot?

Surface plots are created by using ax. plot_surface() function.


1 Answers

The plotting style depends on your data: are you trying to plot a 3D curve (line), a surface, or a scatter of points?

In the first example below I've just used a simple grid of evenly spaced points in the x-y plane for the domain. Generally, you first create a domain of xs and ys, and then calculate the zs from that.

This code should give you a working example to start playing with:

import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import random  def fun(x, y):     return x + y  fig = plt.figure() ax = fig.add_subplot(111, projection='3d') n = 10 xs = [i for i in range(n) for _ in range(n)] ys = list(range(n)) * n zs = [fun(x, y) for x,y in zip(xs,ys)]  ax.scatter(xs, ys, zs)  ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label')  plt.show() 

scatter


For surfaces it's a bit different, you pass in a grid for the domain in 2d arrays. Here's a smooth surface example:

import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import random  def fun(x, y):     return x**2 + y  fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = y = np.arange(-3.0, 3.0, 0.05) X, Y = np.meshgrid(x, y) zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))]) Z = zs.reshape(X.shape)  ax.plot_surface(X, Y, Z)  ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label')  plt.show() 

surface

For many more examples, check out the mplot3d tutorial in the docs.

like image 102
wim Avatar answered Nov 06 '22 14:11

wim