JuliaPlots / StatsPlots.jl

Statistical plotting recipes for Plots.jl
Other
439 stars 90 forks source link

KernelDensity.jl plots are not working #92

Closed juliohm closed 7 years ago

juliohm commented 7 years ago
using KernelDensity
using StatPlots; pyplot()

plot(kde(randn(1000,2)))

PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, kw)) <class 'TypeError'> TypeError('Length of x must be number of columns in z.',) File "/usr/lib/python3.6/site-packages/matplotlib/init.py", line 1898, in inner return func(ax, *args, kwargs) File "/usr/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5825, in contour contours = mcontour.QuadContourSet(self, *args, *kwargs) File "/usr/lib/python3.6/site-packages/matplotlib/contour.py", line 864, in init self._process_args(args, kwargs) File "/usr/lib/python3.6/site-packages/matplotlib/contour.py", line 1429, in _process_args x, y, z = self._contour_args(args, kwargs) File "/usr/lib/python3.6/site-packages/matplotlib/contour.py", line 1508, in _contour_args x, y, z = self._check_xyz(args[:3], kwargs) File "/usr/lib/python3.6/site-packages/matplotlib/contour.py", line 1555, in _check_xyz raise TypeError("Length of x must be number of columns in z.")

piever commented 7 years ago

I'm afraid that there is no recipe for 2D density plots, so you'd need to do something like:

s = kde(randn(1000,2))
heatmap(s.x, s.y, s.density)

to get what you want.

We were planning to add one such recipe here but nobody has made a PR yet.

juliohm commented 7 years ago

@piever what is the recipe at StatPlots.jl for? https://github.com/JuliaPlots/StatPlots.jl/blob/master/src/StatPlots.jl#L18

Also, the snippet of code provided doesn't work like in the recipe.

piever commented 7 years ago

Ups, I was blind. That recipe is only be meant to be used for plots representing a 2D distribution:

heatmap(kde(randn(1000,2)))
contourf(kde(randn(1000,2)))
surface(kde(randn(1000,2)))

When you try plot(kde(randn(1000,2))) it gets translated to

s = kde(randn(1000,2))
plot(s.x, s.y, s.density)

which will not work as a plot command. (EDIT: I was wrong, see below)

What's the issue with the snippet of code I provided above, except being a bit annoying to type all the time?

piever commented 7 years ago

What I meant as a future plan is to have something like density(x, y) being translated to contourf(kde([x y])). It'd be consistent with density(x) for univariate distributions which is implemented already.

piever commented 7 years ago

Actually you were right in that plot(kde(randn(1000,2))) gets translated to contour(kde(randn(1000,2))) that is to say:

s = kde(randn(1000,2))
contour(s.x, s.y, s.density)

But I suspect that it is bugged because it changes the order of x and y. Does

s = kde(randn(1000,2))
contour(s.x, s.y, s.density')

do what you want reliably?

piever commented 7 years ago

Let me know if that solves it so that I can update the recipe at https://github.com/JuliaPlots/StatPlots.jl/blob/master/src/StatPlots.jl#L18

juliohm commented 7 years ago

It works nicely @piever , could you please go ahead and fix the order? Thank you very much.