1

I have a spreadsheet file that I would like to input to create a 3D surface graph using Matplotlib in Python.

I used plot_trisurf and it worked, but I need the projections of the contour profiles onto the graph that I can get with the surface function, like this example.

I'm struggling to arrange my Z data in a 2D array that I can use to input in the plot_surface method. I tried a lot of things, but none seems to work.

Here it is what I have working, using plot_trisurf

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import numpy as np
import pandas as pd

df=pd.read_excel ("/Users/carolethais/Desktop/Dissertação Carol/Códigos/Resultados/res_02_0.5.xlsx")

fig = plt.figure()
ax = fig.gca(projection='3d')
# I got the graph using trisurf 
graf=ax.plot_trisurf(df["Diametro"],df["Comprimento"], df["temp_out"], cmap=matplotlib.cm.coolwarm)

ax.set_xlim(0, 0.5)
ax.set_ylim(0, 100)
ax.set_zlim(25,40)
fig.colorbar(graf, shrink=0.5, aspect=15)
ax.set_xlabel('Diâmetro (m)')
ax.set_ylabel('Comprimento (m)')
ax.set_zlabel('Temperatura de Saída (ºC)')

plt.show()

enter image description here

This is a part of my df, dataframe:

       Diametro  Comprimento   temp_out
0      0.334294     0.787092  34.801994
1      0.334294     8.187065  32.465551
2      0.334294    26.155976  29.206090
3      0.334294    43.648591  27.792126
4      0.334294    60.768219  27.163233
...         ...          ...        ...
59995  0.437266    14.113660  31.947302
59996  0.437266    25.208851  30.317583
59997  0.437266    33.823035  29.405461
59998  0.437266    57.724209  27.891616
59999  0.437266    62.455890  27.709298

I tried this approach to use the imported data with plot_surface, but what I got was indeed a graph but it didn't work, here it's the way the graph looked with this approach: enter image description here Thank you so much

3
  • It's really difficult to help you without any significant info. What have you done, how is your data organized, what have you tried...
    – gboffi
    Feb 20, 2020 at 13:34
  • I'm sorry, it's my first question here. I edited the question with more information if you could take a look I would be very happy. Thank you. Feb 20, 2020 at 14:47
  • It seems that you have 60000 points, are they disposed on a regular grid? 300x200 maybe? If the points are indeed on a regular grid you can reshape your data in the format required for plot_surface
    – gboffi
    Feb 20, 2020 at 21:52

2 Answers 2

1

A different approach, based on re-gridding the data, that doesn't require that the original data is specified on a regular grid [deeply inspired by this example;-].

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as tri
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(19880808)

# compute the sombrero over a cloud of random points
npts = 10000
x, y = np.random.uniform(-5, 5, npts), np.random.uniform(-5, 5, npts)
z = np.cos(1.5*np.sqrt(x*x + y*y))/(1+0.33*(x*x+y*y))

# prepare the interpolator
triang = tri.Triangulation(x, y)
interpolator = tri.LinearTriInterpolator(triang, z)

# do the interpolation
xi = yi = np.linspace(-5, 5, 101)
Xi, Yi = np.meshgrid(xi, yi)
Zi = interpolator(Xi, Yi)

# plotting
fig = plt.figure()
ax = fig.gca(projection='3d')
norm = plt.Normalize(-1,1)
ax.plot_surface(Xi, Yi, Zi,
                cmap='inferno',
                norm=plt.Normalize(-1,1))
plt.show()

enter image description here

1
  • Sorry I didn't reply earlier... It worked. Thank you so much! Apr 7, 2020 at 17:53
1

plot_trisurf expects x, y, z as 1D arrays while plot_surface expects X, Y, Z as 2D arrays or as x, y, Z with x, y being 1D array and Z a 2D array.

Your data consists of 3 1D arrays, so plotting them with plot_trisurf is immediate but you need to use plot_surface to be able to project the isolines on the coordinate planes... You need to reshape your data.

It seems that you have 60000 data points, in the following I assume that you have a regular grid 300 points in the x direction and 200 points in y — but what is important is the idea of regular grid.

The code below shows

  1. the use of plot_trisurf (with a coarser mesh), similar to your code;
  2. the correct use of reshaping and its application in plot_surface;
    note that the number of rows in reshaping corresponds to the number of points in y and the number of columns to the number of points in x;
  3. and 4. incorrect use of reshaping, the resulting subplots are somehow similar to the plot you showed, maybe you just need to fix the number of row and columns.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

x, y = np.arange(30)/3.-5, np.arange(20)/2.-5
x, y = (arr.flatten() for arr in np.meshgrid(x, y))
z = np.cos(1.5*np.sqrt(x*x + y*y))/(1+0.1*(x*x+y*y))

fig, axes = plt.subplots(2, 2, subplot_kw={"projection" : "3d"})
axes = iter(axes.flatten())

ax = next(axes)
ax.plot_trisurf(x,y,z, cmap='Reds')
ax.set_title('Trisurf')

X, Y, Z = (arr.reshape(20,30) for arr in (x,y,z))
ax = next(axes)
ax.plot_surface(X,Y,Z, cmap='Reds')
ax.set_title('Surface 20×30')

X, Y, Z = (arr.reshape(30,20) for arr in (x,y,z))
ax = next(axes)
ax.plot_surface(X,Y,Z, cmap='Reds')
ax.set_title('Surface 30×20')

X, Y, Z = (arr.reshape(40,15) for arr in (x,y,z))
ax = next(axes)
ax.plot_surface(X,Y,Z, cmap='Reds')
ax.set_title('Surface 40×15')

plt.tight_layout()
plt.show()

enter image description here

2
  • Thank you so much, it helped a lot. Now I realize I don't have a regular grid. My x has 1000 unique numbers and my y has 60000. The numbers are very close to each other (like 0.00112 - 0.00113), but they are not exactly the same. So, in order to overcome this issue I need to do something like this: matplotlib.org/3.1.1/gallery/images_contours_and_fields/… ? Feb 21, 2020 at 11:00
  • The "irregular data grid" looks like the way to go... I'll post an example
    – gboffi
    Feb 21, 2020 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.