# Plotting surface with plots and pyplot

**URL:** <https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154>\
**Category:** General Usage\
**Tags:** package, plots\
**Created:** [January 18, 2023, 1:53pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154 "2023-01-18T13:53:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![neda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neda/32/45939_2.png) [@neda](https://discourse.julialang.org/u/neda)\
**Post date:** [January 18, 2023, 1:53pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/1 "2023-01-18T13:53:03Z")

</div>

I am trying to plot a 3D object with using Plots or PyPlot with the command surface. When I utilize Plots and surface(x,y,z) where x,y,z are three vectors with the same size, I get the message:  
\*\*\* IDENTICAL DATA POINTS.  
NDP = 100 IP1 = 1 IP2 = 2 XD=0 YD=0  
ERROR DETECTED IN ROUTINE IDTANG.  
libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast\_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86\_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix \_dri)  
libGL error: failed to load driver: swrast  
X Error of failed request: BadMatch (invalid parameter attributes)  
Major opcode of failed request: 148 (GLX)  
Minor opcode of failed request: 26 (X\_GLXMakeContextCurrent)  
Serial number of failed request: 27  
Current serial number in output stream: 27

Also, when I use PyPlot and surf command, I get the message:  
PyError ($(Expr(:escape, :(ccall(#= /home/neda/.julia/packages/PyCall/ygXW2/src/pyfncall.jl:43 =# @pysym(:PyObject\_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) \<class ‘RuntimeError’\>  
RuntimeError(‘Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2); use python verbose option (-v) to see original qhull error.’)  
File “/home/neda/.julia/conda/3/lib/python3.10/site-packages/mpl\_toolkits/mplot3d/axes3d.py”, line 1770, in plot\_trisurf  
Triangulation.get\_from\_args\_and\_kwargs(\*args, \*\*kwargs)  
File “/home/neda/.julia/conda/3/lib/python3.10/site-packages/matplotlib/tri/triangulation.py”, line 161, in get\_from\_args\_and\_kwargs  
triangulation = Triangulation(x, y, triangles, mask)  
File “/home/neda/.julia/conda/3/lib/python3.10/site-packages/matplotlib/tri/triangulation.py”, line 58, in **init**  
self.triangles, self.\_neighbors = \_qhull.delaunay(x, y)

Please let me know that does anyone know the problem?  
Thank you so much in advance.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2023, 2:18pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/2 "2023-01-18T14:18:18Z")

</div>

> [@neda](#):
>
> \*\*\* IDENTICAL DATA POINTS.  
> XD=0 YD=0

There are two points with the same X-Y coordinates, they must all be different.

---

<div class="post-metadata">

**Author:** ![neda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neda/32/45939_2.png) [@neda](https://discourse.julialang.org/u/neda)\
**Post date:** [January 18, 2023, 2:38pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/3 "2023-01-18T14:38:03Z")

</div>

Thank you Rafael for the reply. I checked the code and tried it with different x and y as the input for surface and the error is:  
surface(x,y,z)  
libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast\_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86\_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix \_dri)  
libGL error: failed to load driver: swrast  
X Error: BadMatch  
Request Major code 148 (GLX)  
Request Minor code 26 ()  
Error Serial #27  
Current Serial #27

I tried the command surface in a very simple code:

using LinearAlgebra

using Plots; plotyjs()

r = collect(Float16, range(0, length=100, stop=1));

theta = collect(Float16, range(0, length=100, stop=2\*pi));

len =100

a = zeros(len); x = zeros(len); y = zeros(len)

A = fill(zeros(2), len)

for i in 1:len

x[i] = r[i]\*cos(theta[i])

y[i] = r[i]\*sin(theta[i])

end

z = zeros(Float16, len)

for i in 1:len

z[i] = x[i]^2 + y[i]^2

end

surface(x,y,z)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2023, 3:06pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/4 "2023-01-18T15:06:22Z")

</div>

OK, now it seems to be a new problem and I have no idea.

NB:

- Here, your code runs fine and a surface can be displayed with gr() backend.
- If I am not mistaken, plotlyjs() `surface()` expects a regular x-y mesh of points
- use triple backticks around your code:  
```  
_type the code here_  
```

---

<div class="post-metadata">

**Author:** ![neda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neda/32/45939_2.png) [@neda](https://discourse.julialang.org/u/neda)\
**Post date:** [January 18, 2023, 3:24pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/5 "2023-01-18T15:24:21Z")

</div>

“”  
‘’’ using LinearAlgebra  
using Plots; gr()

r = collect(Float16, range(0, length=100, stop=1));  
theta = collect(Float16, range(0, length=100, stop=2\*pi));  
len =100  
a = zeros(len); x = zeros(len); y = zeros(len)

for i in 1:len  
x[i] = r[i]\*cos(theta[i])  
y[i] = r[i]\*sin(theta[i])  
end  
z = zeros(Float16, len)  
for i in 1:len  
z[i] = x[i]^2 + y[i]^2  
end  
surface(x,y,z)‘’’

I tried the gr() backend as you see in the above code. The error is the same as the before: libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast\_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86\_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix \_dri)  
libGL error: failed to load driver: swrast  
X Error: BadMatch  
Request Major code 148 (GLX)  
Request Minor code 26 ()  
Error Serial #27  
Current Serial #27

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [January 18, 2023, 3:48pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/6 "2023-01-18T15:48:27Z")

</div>

From you code it seems that you intend to plot a paraboloid of equation z= x^2+y^2, using a parameterization, of parameters r and theta. You can avoid using for loops, as follows:

```julia
using Plots; plotlyjs()
r = collect(Float16, range(0, 1, length=100));
theta = collect(Float16, range(0, 2*pi, length=100));
x = r .* cos.(theta)' #outer product of the column r with the row, cos.(theta)'
y = r .*sin.(theta)'
z = r.^2 .* ones(size(theta))' #x^2=y^2=r^2
surface(x,y,z)

```

In Linear Algebra the outer product is the product of a column with a row vector, and the result is a Matrix.  
Check by printing:

```julia
print(size(x), size(y), size(z))

```

---

<div class="post-metadata">

**Author:** ![neda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neda/32/45939_2.png) [@neda](https://discourse.julialang.org/u/neda)\
**Post date:** [January 18, 2023, 4:18pm UTC](https://discourse.julialang.org/t/plotting-surface-with-plots-and-pyplot/93154/7 "2023-01-18T16:18:04Z")

</div>

> [@empet](#):
>
> ```julia-auto
> using Plots; plotlyjs()
> r = collect(Float16, range(0, 1, length=100));
> theta = collect(Float16, range(0, 2*pi, length=100));
> x = r .* cos.(theta)' #outer product of the column r with the row, cos.(theta)'
> y = r .*sin.(theta)'
> z = r.^2 .* ones(size(theta))' #x^2=y^2=r^2
> surface(x,y,z)
> 
> ```

Thanks a lot
