Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly plot_trisurf isn't working with arange arrays

I've basically just copied the example code found on the Matplotlib website, but I replaced their radii and angles with simple arange arrays.

I've tried different array functions and I can't seem to figure out anything.

from mpl_toolkits.mplot3d import Axes3D  

import matplotlib.pyplot as plt
import numpy as np
from Equation import Expression


x = np.arange(0,100,0.01)
y = np.arange(0,100,0.01)
x2 = np.append(0,x.flatten())
y2 = np.append(0,y.flatten())
z = x2 + y2
print(z)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
plt.show()

I'm just trying to make a graph of z = x + y but I'm getting a confusing error.

"RuntimeError: Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2); use python verbose option (-v) to see original qhull error."

Edit: I've also tried it without calling flatten() but I get the same result though.

like image 681
Kljunar Man Avatar asked Oct 28 '25 05:10

Kljunar Man


1 Answers

The error you are getting is because your z is not a surface but a line. You need to use at least 3 points that would define a plane. One option could be to use np.meshgrid to create your surface for plotting and then flatten everything to insert into the function. Try going back to some example code here. Note you may also want to change your resolution depending on the detail of your surface.

from mpl_toolkits.mplot3d import Axes3D  

import matplotlib.pyplot as plt
import numpy as np


x = np.arange(0,100,1)
y = np.arange(0,100,1)
x2 = np.append(0,x.flatten())
y2 = np.append(0,y.flatten())

x2,y2 = np.meshgrid(x2,y2) #This is what you were missing

z = x2 + y2

fig = plt.figure(figsize=(12,12))
ax = fig.gca(projection='3d')
ax.plot_trisurf(x2.flatten(), y2.flatten(), z.flatten(), linewidth=0.2, antialiased=True) #flatten all the arrays here


plt.show()

enter image description here

like image 72
BenT Avatar answered Oct 29 '25 18:10

BenT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!