Building a 2d array using mplot3d

I have a two-dimensional numpy array and I want to build it in 3D. I heard about mplot3d but i cant work fine

Here is an example of what I want to do. I have an array with sizes (256,1024). He should build a 3D graph where the x axis is from 0 to 256, the y axis is from 0 to 1024, and the z axis of the graph displays the value of the array in each record.

How can I do it?

+10
source share
4 answers

It looks like you are trying to create a surface graph (alternatively you can draw a wireframe or a completed invoice graph .

From the information in the question, you can try something like:

import numpy import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # Set up grid and test data nx, ny = 256, 1024 x = range(nx) y = range(ny) data = numpy.random.random((nx, ny)) hf = plt.figure() ha = hf.add_subplot(111, projection='3d') X, Y = numpy.meshgrid(x, y) # `plot_surface` expects `x` and `y` data to be 2D ha.plot_surface(X, Y, data) plt.show() 

Obviously, you need to choose more reasonable data than using numpy.random to get a reasonable surface.

+12
source

You can find the answer in one example Matplotlib Gallery ; 3D examples are coming to an end.

More generally, the Matplotlib Gallery is an excellent first-stop resource for determining how to draw some graphics.

In the examples I examined, they basically work with three 2D arrays: one with all x values, one with all y values ​​and the last with all z values. So, one solution is to create arrays of x and y values ​​(for example, meshgrid() ).

0
source

You can also use the oct2py module, which is actually a python octave bridge. Using it, you can use octave fucntions, and you can get what you need, and that is pretty easy too.

check out this documentation: https://www.gnu.org/software/octave/doc/v4.0.1/Three_002dDimensional-Plots.html

And for an example example:

 from oct2py import octave as oc tx = ty = oc.linspace (-8, 8, 41) [xx, yy] = oc.meshgrid (tx, ty) r = oc.sqrt (xx * xx + yy * yy) + oc.eps() tz = oc.sin (r) / r oc.mesh (tx, ty, tz) 

The above is python code that is similar to the first example implemented in an octave in the above documentation.

0
source

You can try the 3D plot using the bar3d function.

Suppose you have an array A of dimension (25, 10), the value with index (i, j) is A [i] [j]. The following code example can give you a 3D bar chart where the height of each bar is A [i] [j].

 from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np %matplotlib inline np.random.seed(1234) fig = plt.figure() ax1 = fig.add_subplot(111, projection='3d') A = np.random.randint(5, size=(25, 10)) x = np.array([[i] * 10 for i in range(25)]).ravel() # x coordinates of each bar y = np.array([i for i in range(10)] * 25) # y coordinates of each bar z = np.zeros(25*10) # z coordinates of each bar dx = np.ones(25*10) # length along x-axis of each bar dy = np.ones(25*10) # length along y-axis of each bar dz = A.ravel() # length along z-axis of each bar (height) ax1.bar3d(x, y, z, dx, dy, dz) 

On my pc with random 1234 seed, I get the following plot: enter image description here

However, it might be slow to create a graph for your size problem (256, 1024).

0
source

All Articles