Help to perform scatter density plot with Plots.jl

Dear all,

I would like to perform a scatter density plot with Plots.jl, such as: scatter density:


Two way explored :

  1. package : 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.


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?

Thanks for answering.
Histogram2d plot generates some kind of pixelisation that I would like to avoid:

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). GitHub - gher-ulg/DIVAnd.jl: DIVAnd performs an n-dimensional variational analysis of arbitrarily located observations

1 Like

The closest with Plots is available via GR, directly.

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).

I think the command which you are looking for is:

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

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

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:
#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)



  1. Second one with contourf:
#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))

	 c = :vik,
	 linewidth = 0)