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:

gaussian

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.

Regards

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:
histogram2d_discourse

1 Like

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

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

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

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)

savefig("./heatmap.png");

heatmap

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


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

savefig("./contourf.png");

contourf

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

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