# Plotting scatter points together with surface plot

**URL:** https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063
**Category:** Visualization
**Tags:** plots, makie
**Created:** [August 11, 2024, 5:22pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063 "2024-08-11T17:22:59Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 11, 2024, 5:22pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/1 "2024-08-11T17:22:59Z")

</div>

Hello!

I have a function that i want to surface plot and a bunch of points that i want to add to that plot. Unfortunately this doesn’t look good the way i do it.

The problem appears to be that i first do the surface plot and afterwards i plot the scatter points on top of it.  
Is there a more elegant way/ a different package i should use to receive a better looking plot (e.g. where the points are not just thrown on top of the surface plot but more “embedded”)?

In the following i give a working example of code - and a resulting plot.

```julia
using Plots
let
A=rand(Uniform(-3,3),2,10) 
f(x,y)=-((1+cos(12sqrt(x^2+y^2)))/(0.5*(x^2+y^2)+2))

t1 = range(-5.12, 5.12, length=200)
t2 = range(-5.12,5.12, length=200)

# Creating Surface-Plot
surface_plot = surface(t1, t2, f, legend=false)

#Add scatter plots
scatter_x=A[1,:]
scatter_y=A[2,:]
scatter_z= [f for (t1, t2) in zip(scatter_x, scatter_y)]
scatter_points=scatter!(scatter_x, scatter_y, scatter_z, marker=:circle, markersize=5)

display(surface_plot)
end

```

yields:

 ![Screenshot 2024-08-11 at 19.15.37](https://global.discourse-cdn.com/julialang/original/3X/f/e/feeb33f30d736aec7cb60151c685f7f610b6469a.png)

---

<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: [August 11, 2024, 6:19pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/2 "2024-08-11T18:19:00Z")

</div>

In Plots.jl 's, it should look better using `pythonplot()` or `plotlyjs()` backends, and _cerise sur le gâteau_, you will get interactive windows.

---

<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: [August 11, 2024, 6:56pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/3 "2024-08-11T18:56:53Z")

</div>

And welcome.

Probably this code here:

> [@MoritzD](#):
>
> ```julia
> scatter_z= [f for (t1, t2) in zip(scatter_x, scatter_y)]
> 
> ```

may be replaced by:

```julia
scatter_z = f.(scatter_x, scatter_y)

```

This Julia feature is known as _dot broadcasting_ - see the [reference blog post here](https://julialang.org/blog/2017/01/moredots/).

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 12, 2024, 9:04am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/4 "2024-08-12T09:04:17Z")

</div>

Both of your replies work and help me, thanks!

Is there a way to have the markershape as a ball/sphere instead of a circle?  
I couldn’t find that in the documentation, my intuitive approach would be to make a new function that produces a ball/sphere and use surface to draw that ball/sphere.

 ![Screenshot](https://global.discourse-cdn.com/julialang/original/3X/8/6/86dc23821824812ece29180fb0a12ee59015de0a.png)

---

<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: [August 12, 2024, 9:20am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/5 "2024-08-12T09:20:10Z")

</div>

> [@MoritzD](#):
>
> Is there a way to have the markershape as a ball/sphere instead of a circle?

This is possible in Makie.jl - see for instance [here](https://stackoverflow.com/a/64187673/14071775).

Using Plots.jl, I don’t know, except plotting N spheres ([example here](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978/2)).

---

<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: [August 12, 2024, 11:45am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/6 "2024-08-12T11:45:22Z")

</div>

Using Plots.jl 's `plotlyjs()` backend, the sphere function in code linked above and:

```julia
surface(t1, t2, f, legend=false, lims=(-pi, pi), size=(1000,1000))
surface!([sphere(0.1, ri) for ri in zip(scatter_x, scatter_y, scatter_z)], c=:red)

```

we can produce:

 ![Plots_plotlyjs_scattered_spheres_on_top_of_surface](https://global.discourse-cdn.com/julialang/original/3X/7/b/7ba061c905a2e37bfc6bcfcb262d05097806be53.jpeg)

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 12, 2024, 12:06pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/7 "2024-08-12T12:06:13Z")

</div>

Thank you for your help!  
i tried it with Makie.jl now, this appears to work but somehow my scatterpoints are stretched.

![Bildschirmfoto 2024-08-12 um 14.02.12](https://global.discourse-cdn.com/julialang/original/3X/a/4/a4000275add38f6319d4ffe5c675392cbf7c07db.png)

My code:

```julia
using Makie
using Distributions  
using GLMakie  

let
    fig = Figure(fontsize = 22)
    ax3d = Axis3(fig[1, 1];aspect = (1, 1, 1))

    A=rand(Uniform(-3,3),2,10) 
    f(x,y)=-((1+cos(12sqrt(x^2+y^2)))/(0.5*(x^2+y^2)+2))
    
    t1 = range(-5.12, 5.12, length=200)
    t2 = range(-5.12,5.12, length=200)

    x=A[1,:]
    y=A[2,:]
    z= f.(x,y) 
      
    pltobj = surface!(ax3d,t1, t2, f)
    meshscatter!(x, y, z,markersize=0.2)
    
    display(fig)
end

```

---

<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: [August 12, 2024, 12:06pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/8 "2024-08-12T12:06:47Z")

</div>

Set the aspect ratio to 1.

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 12, 2024, 2:13pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/9 "2024-08-12T14:13:47Z")

</div>

I did in line 7.

---

<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: [August 12, 2024, 2:20pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/10 "2024-08-12T14:20:18Z")

</div>

Not a regular user of Makie, but would try:  
`meshscatter!(ax3d, ...)`

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 12, 2024, 2:21pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/11 "2024-08-12T14:21:34Z")

</div>

I tried, the result is the same.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [August 12, 2024, 2:34pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/12 "2024-08-12T14:34:41Z")

</div>

It needs to be `aspect = :data` as shown here [Axis3 | Makie](https://docs.makie.org/stable/reference/blocks/axis3#aspect)

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 12, 2024, 2:43pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/13 "2024-08-12T14:43:46Z")

</div>

Thanks, but this results in a plot like this:  
 ![Screenshot](https://global.discourse-cdn.com/julialang/original/3X/3/2/32a3368e6705c9dadb388e9002f24d16083a2aa3.png)

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 12, 2024, 3:07pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/14 "2024-08-12T15:07:29Z")

</div>

we need `scale_marker=false` for meshscatter…

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [August 12, 2024, 3:13pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/15 "2024-08-12T15:13:48Z")

</div>

But how far would the sphere mesh extend into the surrounding mesh then? The screen space sized marker is easier to understand in 2d I think.

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 13, 2024, 11:59am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/16 "2024-08-13T11:59:25Z")

</div>

Ultimately i want the markers to have different sizes.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [August 13, 2024, 12:04pm UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/17 "2024-08-13T12:04:57Z")

</div>

One simple thing you could do now is to pick the markersize depending on the limits of the Axis3, as long as you don’t have nonlinear axis transformations that would work. So let’s say the z axis has a scaling factor of 2 vs x and y then you could do `markersize = Vec(ms, ms, 0.5ms)`. You could lift `ax.finallimits` to compute the scaling factor, and correct for the `aspect` too if that’s not 1, 1, 1.

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 14, 2024, 10:33am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/18 "2024-08-14T10:33:48Z")

</div>

I am not quite sure if i understand what you mean with that. How is `ms` defined?

---

<div class="post-metadata">

### Author: ![MoritzD](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@MoritzD](https://discourse.julialang.org/u/MoritzD)
#### Post date: [August 14, 2024, 10:39am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/19 "2024-08-14T10:39:39Z")

</div>

I tried with the sphere approach, unfortunately i get the same issue as with makie:

 ![Screenshot](https://global.discourse-cdn.com/julialang/original/3X/f/4/f431ce7a7d1a43fb30605d29b1e4ab8b3e533dfb.png)

If i adjust the zaxis with (zlims=(-1,1)) the markers are stretched again:

 ![Screenshot](https://global.discourse-cdn.com/julialang/original/3X/b/9/b9fe6baff57f9fdd5216dc667d373e6f879f0e3b.png)

---

<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: [August 14, 2024, 11:05am UTC](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063/20 "2024-08-14T11:05:19Z")

</div>

This is not an issue, this is the correct display for aspect ratio = 1. If you want the same approach for a different aspect ratio, we need to draw ellipsoids that will look like spheres if the principal axes are inversely proportional to the aspect ratio.

[Next page](https://discourse.julialang.org/t/plotting-scatter-points-together-with-surface-plot/118063.md?page=2)
