GeorgM - 1 year ago 137

Python Question

enter image description herei am a beginner at programming. I started like 2 months ago so please be patient with me :-)

So i want to make a 3d surface plot with matplotlib on python 3.4.

i watched a lot of tutorials about this but i didnt find something exactly like what i need to do..i hope you can help me.

In all the videos they give with meshgrid a relation between the 3 axis(x, y, z) but i dont want that.what i want to do is this:

i have 16 sensors and they are placed on 4 rows with 4 sensors each so in the fisrt row are 1,2,3,4 and the second 5,6,7,8 and so on..(the order of sensors is very important).for example Sensor Number 4 = 200 from a skala from 0 until 800..i thought of using x and y axis only for the correct location in the graph..for example with sensor 4(=200 from 800) is placed in the first row in the forth column...so..x=4 and y=1 and z=200 from 800 like before.so in the end every sensor has only one 'real' value..z..

how can i import this kind of data with matplotlib for all 16 sensor to make a 3d plot?? i would really appreciate any kind of help..

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You need to start somewhere. So let's say the data is a list of 16 values. You can then create a 2D array of it and show that array as an image.

```
import numpy as np
import matplotlib.pyplot as plt
# input data is a list of 16 values,
# the first value is of sensor 1, the last of sensor 16
input_data = [200,266,350,480,
247,270,320,511,
299,317,410,500,
360,360,504,632]
# create numpy array from list and reshape it to a 4x4 matrix
z = np.array(input_data).reshape(4,4)
# at this point you can already show an image of the data
plt.imshow(z)
plt.colorbar()
plt.show()
```

An option to now plot the values as height in a 3D plot instead of color in a 2D plot would be to use a `bar3d`

plot.

```
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# input data is a list of 16 values,
# the first value is of sensor 1, the last of sensor 16
input_data = [200,266,350,480,
247,270,320,511,
299,317,410,500,
360,360,504,632]
# create a coordinate grid
x,y = np.meshgrid(range(4), range(4))
ax = plt.gcf().add_subplot(111, projection="3d")
#plot the values as 3D bar graph
# bar3d(x,y,z, dx,dy,dz)
ax.bar3d(x.flatten(),y.flatten(),np.zeros(len(input_data)),
np.ones(len(input_data)),np.ones(len(input_data)),input_data)
plt.show()
```

You may also plot a surface plot, but in that case the grid will define the edges of the surface tiles.

```
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# input data is a list of 16 values,
# the first value is of sensor 1, the last of sensor 16
input_data = [200,266,350,480,
247,270,320,511,
299,317,410,500,
360,360,504,632]
# create a coordinate grid
x,y = np.meshgrid(range(4), range(4))
z = np.array(input_data).reshape(4,4)
ax = plt.gcf().add_subplot(111, projection="3d")
#plot the values as 3D surface plot
ax.plot_surface(x,y,z)
plt.show()
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**