# Surface plotting using PyPlot

**URL:** <https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417>\
**Category:** General Usage\
**Created:** [February 1, 2021, 8:46pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417 "2021-02-01T20:46:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [February 1, 2021, 8:46pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/1 "2021-02-01T20:46:45Z")

</div>

Hi,

I want to use PyPlot (I don’t like Plots) for plotting a surface, but I seem to miss how this is done.  
I have a 1D-array of x values, a 1D-array of y values, and a 2D array z of f(x,y) values. In this case length(x)=length(y) and size(z)=(length(x),length(y)).  
What is the proper way to generate the 3D-plot? I can’t understand the logic of things like plot\_surface(x,y,z) or plot\_wireframe(x,y,z), where the help states that x,y,z must be 2D arrays… why 2D-arrays? x and y are 1D values, right?  
Ok something escapes me. Nay help would be great 🙂

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 1, 2021, 9:10pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/2 "2021-02-01T21:10:15Z")

</div>

Please give a MWE that is copy-pastable and tell us what you have tried so far that doesn’t work.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [February 1, 2021, 9:48pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/3 "2021-02-01T21:48:08Z")

</div>

something like this perhaps

```julia
x = range(-3,stop=3,length=20)
y = range(1,stop=7,length=25)

z = fill(NaN,(length(x),length(y))) #to put results in, initialized as NaNs
for j = 1:length(y) #create z column by column
  z[:,j] = 2*x.^2 .+ (y[j]-4)^2
end

#notice the arguments: x,y,z'
println(size(x),size(y),size(z'))

figure()
  surf(x,y,z',rstride=1,cstride=1,cmap=ColorMap("cool"),alpha=0.8)

```

Also, see [Plots Gallery | [“Julia Plots Gallery”]](https://goropikari.github.io/PlotsGallery.jl/)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 1, 2021, 10:25pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/4 "2021-02-01T22:25:57Z")

</div>

I’m not sure why you “don’t like Plots” since the following just works in Plots.jl:

```julia
julia> using Plots

julia> x = y = -1:0.1:1;
1:10

julia> z = x .^ 2 .+ y' .^ 2;

julia> surface(x, y, z)

```

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 1, 2021, 11:48pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/5 "2021-02-01T23:48:43Z")

</div>

> [@Ferran\_Mazzanti](#):
>
> help states that x,y,z must be 2D arrays… why 2D-arrays? x and y are 1D values, right?

Haven’t used it in a while but indeed I kind of remember this being the case, but maybe it was changed to allow 1D arrays, e.g. on matplotlib 3.2.2 / PyPlot 2.9.0 basically the identical to the Plots code works:

```julia
julia> using PyPlot

julia> x = y = -1:0.1:1

julia> z = x .^ 2 .+ y' .^ 2

julia> plot_surface(x, y, z)

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [February 2, 2021, 1:07am UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/6 "2021-02-02T01:07:28Z")

</div>

In python those functions receive 2D arrays for the coordinates (unless they have changed it in recent versions as mentioned by @marius311). This is, you specify the x and y positions for each point of the array, and not only for each row and each column. A function that is useful to build those arrays in python was called if I remember meshgrid. You can mock this function as expressed in [Utilizing ndgrid/meshgrid functionality in Julia - Stack Overflow](https://stackoverflow.com/a/63224416/2213815:)

```julia
function meshgrid(x, y)
    X = [i for i in x, j in 1:length(y)]
    Y = [j for i in 1:length(x), j in y]
    return X, Y
end

```

Then you can do:

```julia
x = range(-3,stop=3,length=20)
y = range(1,stop=7,length=25)

x_grd,y_grd = meshgrid(x, y)

z = fill(NaN,(length(x),length(y))) #to put results in, initialized as NaNs
for j = 1:length(y) #create z column by column
  z[:,j] = 2*x.^2 .+ (y[j]-4)^2
end

#notice the arguments: x,y,z'
println(size(x),size(y),size(z'))

figure()
  surf(x_grd,y_grd,z',rstride=1,cstride=1,cmap=ColorMap("cool"),alpha=0.8)

```

This sometimes is a waste of memory, but it will work fine.

---

<div class="post-metadata">

**Author:** ![mgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgr/32/8624_2.png) [@mgr](https://discourse.julialang.org/u/mgr)\
**Post date:** [February 2, 2021, 1:28am UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/7 "2021-02-02T01:28:06Z")

</div>

A recent discussion on why one doesn’t need meshgrid in Julia is at [Meshgrid function in Julia](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/3)

---

<div class="post-metadata">

**Author:** ![Yuktee\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuktee_gupta/32/27063_2.png) [@Yuktee\_Gupta](https://discourse.julialang.org/u/Yuktee_Gupta)\
**Post date:** [July 12, 2021, 7:17pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/8 "2021-07-12T19:17:13Z")

</div>

I seem to be having a similar problem as the OP, but I cannot figure out a solution for unequal x and y array lengths, the `.^ or .*` operators don’t work, or as suggested as a replacement to meshgrid, using a z = [f(xs, ys) for xs in x, ys in y] and then plotting doesn’t work either.

---

<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:** [July 12, 2021, 7:30pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/9 "2021-07-12T19:30:40Z")

</div>

@Yuktee_Gupta, have you checked [this post](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/15)?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 12, 2021, 9:56pm UTC](https://discourse.julialang.org/t/surface-plotting-using-pyplot/54417/10 "2021-07-12T21:56:13Z")

</div>

Welcome to the Julia Discourse!

Please post a MWE, i.e. a Minimal Working Example (or in this case, a non-working example!) – in other words, a simple version of the data you want to plot and what exactly you tried.
