# Help to perform scatter density plot with Plots.jl

**URL:** <https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [December 8, 2021, 8:49pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790 "2021-12-08T20:49:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [December 8, 2021, 8:49pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/1 "2021-12-08T20:49:49Z")

</div>

Dear all,

I would like to perform a scatter density plot with Plots.jl, such as: [scatter density](https://github.com/astrofrog/mpl-scatter-density):

![gaussian](https://global.discourse-cdn.com/julialang/original/3X/4/6/46c95d3d277ec06db5457879f264cdcded7e425b.png)

Two way explored :

1. package : [KernelDensity.jl](https://github.com/JuliaStats/KernelDensity.jl), usable to perform some kind of contour plots;
2. There is also the histogram2d. However there are some discontinuities.

I do not know exactly how to manage it with scatter plot? Any idea will be helpful.

Regards

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 8, 2021, 9:24pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/2 "2021-12-08T21:24:33Z")

</div>

Scatter plot is not the word you’re looking for.

Depending on the structure of your data I would suggest `histogram2d` or `heatmap`. What’s your issue with those?

---

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [December 9, 2021, 8:19am UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/3 "2021-12-09T08:19:21Z")

</div>

Thanks for answering.  
Histogram2d plot generates some kind of pixelisation that I would like to avoid:  
 ![histogram2d_discourse](https://global.discourse-cdn.com/julialang/original/3X/f/6/f60f65cb9ee6a17141390df07b086af150785a47.png)

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [December 9, 2021, 8:47am UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/4 "2021-12-09T08:47:48Z")

</div>

heatmaps or kernel density estimates are indeed what you are looking for. If your domain has special boundaries or particular topologies you might want to look into heatmaps provided within DIVAnd (DIVAnd\_heatmap). [https://github.com/gher-ulg/DIVAnd.jl](https://github.com/gher-ulg/DIVAnd.jl)

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [December 9, 2021, 11:49am UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/5 "2021-12-09T11:49:11Z")

</div>

The closest with Plots is available via GR, directly.

> <https://twitter.com/josef_heinen/status/1057670563366404097>

> <https://github.com/jheinen/GR.jl/blob/master/examples/shade_ex.jl>

there you have the option to control also the resolution.

I do have also have a Makie solution if you are interested (still cleaning up some parts).

---

<div class="post-metadata">

**Author:** ![ayushsengupta8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayushsengupta8/32/31757_2.png) [@ayushsengupta8](https://discourse.julialang.org/u/ayushsengupta8)\
**Post date:** [December 9, 2021, 12:19pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/6 "2021-12-09T12:19:44Z")

</div>

Hello,  
I think the command which you are looking for is:

using Plots  
f(x) = x^2 #just for example sake  
gr()  
scatter!(x,y, label=“points”)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 9, 2021, 3:54pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/7 "2021-12-09T15:54:20Z")

</div>

You can increase the number of bins to reduce the granularity. But if you want it to be smooth you should make a distribution fit and 9plot the distribution with `heatmap`

---

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [December 10, 2021, 2:09pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/8 "2021-12-10T14:09:13Z")

</div>

Thanks a lot for answering.

I prefer to use Plots.jl in order to keep plot formatting (size, font, colors…). However, I am impressed of the plot speed with GR.

I explored two ways:

1. First one with heatmap as suggested:

```julia
#test heatmap
using KernelDensity
using Interpolations
using Plots

x = sort(randn(5000))
y = x * 2.3 + sort(randn(5000))

k = kde((x, y))
ik = InterpKDE(k)

z = pdf(ik, x, y)

heatmap(x, y, z, c = :vik)

savefig("./heatmap.png");

```

![heatmap](https://global.discourse-cdn.com/julialang/original/3X/1/b/1bf3c68315753697c3b7c1790846f82b2a9e5578.png)

1. Second one with contourf:

```julia
#test contourf linewidth 0
using KernelDensity
using Plots
using StatsPlots

x = sort(randn(5000))
y = x * 2.3 + sort(randn(5000))

k = kde((x, y))

contourf(k,
	 c = :vik,
	 linewidth = 0)

savefig("./contourf.png");

```

![contourf](https://global.discourse-cdn.com/julialang/original/3X/a/f/af84afc892b45fbd6051ff6bb75a914f23cfcac9.png)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 17, 2023, 5:19pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/9 "2023-10-17T17:19:59Z")

</div>

Sorry to revive an old topic. But any suggestions soon how to do this, with log-scale on both axes?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 17, 2023, 5:24pm UTC](https://discourse.julialang.org/t/help-to-perform-scatter-density-plot-with-plots-jl/72790/10 "2023-10-17T17:24:59Z")

</div>

Does `heatmap(...; xscale=:log, yscale=:log)` work?
