2
votes

I am trying to use Julia to create a gif animation showing the change of density of data points with time (the data points are at the beginning concentrated at the center, and than spread to the sides, a little bit like 2D Gaussian of variance increasing with time). I have checked a catalogue of available kinds of plots in Julia:

http://docs.juliaplots.org/latest/examples/gr/

And I have tried contour plot, heatmap and 2D histogram. However, it seems that the grids of a heatmap or a contour plot have to be manually specified which is highly inconvenient. A 2D histogram serves the purpose better, but it's more related to the number of data points and when I want the plot to be more continuous by setting more bins, it cannot describe the density of data points well. Are there any good substitutes of the 2D density plot in matplotlib in Julia as the following?

https://python-graph-gallery.com/85-density-plot-with-matplotlib/

1
The best approach if you know what you want from python might be to use matplotlib, which is fully accessible in Julia as the PyPlot.jl package. Alternatively you can always use KernelDensity.jl to fit a 2d kernel density yourself, sample an array over it, then plot that with heatmap or contour. Indeed it would be nice to have a recipe doing that in StatsPlots.jlMichael K. Borregaard
I've opened an issue for that on StatsPlotsMichael K. Borregaard
Thank you, problem solved. I plotted it by applying a Gaussian Kernel Density.Sato
@Michael, can you post the comments as an answer since it solved the issue?logankilpatrick

1 Answers

2
votes

You use a package like KernelDensity to calculate the point density, then plot that. Here's an example

using StatsPlots, KernelDensity
a, b = randn(10000), randn(10000)
dens = kde((a,b))
plot(dens)

enter image description here

The philosophy, in the Plots package and other places in Julia, is that you generate the object you are interested in first, and then dispatch takes care of plotting it correctly.

Alternatively, you can always use PyPlot to plot anything using matplotlib directly.