Skip to content

BUG: rplot.GeomDensity2D flips axes #3753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flo-rian opened this issue Jun 4, 2013 · 7 comments
Closed

BUG: rplot.GeomDensity2D flips axes #3753

flo-rian opened this issue Jun 4, 2013 · 7 comments
Labels
Milestone

Comments

@flo-rian
Copy link

flo-rian commented Jun 4, 2013

The axes drawn by rplot.GeomDensity2D are flipped.
line 565 reads:
ax.contour(Z, extent=[x_min, x_max, y_min, y_max])
but should read:
ax.contour(Z.T, extent=[x_min, x_max, y_min, y_max])
The following test code gives the results shown below:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas.tools.rplot as rplot

x = np.arange(1,30)
y = x*x
x = x + np.random.normal(size=x.shape)
y = y + np.random.normal(size=y.shape)
data = data = pd.DataFrame({'x': x, 'y': y})
plot = rplot.RPlot(data, x='x', y='y')
plot.add(rplot.GeomScatter())
plot.add(rplot.GeomDensity2D())
plot.add(rplot.GeomPolyFit(degree=2))
plot.render(plt.gcf())
plt.show()

density_as_is

density_fixed

@cpcloud
Copy link
Member

cpcloud commented Jul 21, 2013

@flo-rian do you want to submit a pr for this?

@flo-rian
Copy link
Author

No, I am new to github and tried to quickly do that but it seems too
much efford for a one line fix.
Can someone else get the fix in the codebase?

Thanks,
Florian

On 07/21/2013 02:07 AM, Phillip Cloud wrote:

@flo-rian https://github.com/flo-rian do you want to submit a pr for this?


Reply to this email directly or view it on GitHub
#3753 (comment).

@cpcloud
Copy link
Member

cpcloud commented Jul 22, 2013

@flo-rian if u have the fix on github i can pull down, do whatever git surgery needs to be done and put it in the codebase for you 😄 all the while retaining authorship

@cpcloud
Copy link
Member

cpcloud commented Jul 22, 2013

i see that the fix could be just transposition but are you sure that doesn't break anything else? seemingly trivial things can break other things in a non trivial way. in this case you're probably right, but it's good to be sure

@flo-rian
Copy link
Author

Dear Phillip,

thanks for the 'github surgery offer' :-)
I did 'fix' it in github yesterday night but found the commit guideline
after that so my comment does not follow the guidelines.
I'll delete my fork, refork it, make the little change and have a
proper comment and then you can pull it down.

Regarding the breaking things: I just transpose the Z array that is fed
into the ax.contour
method by suppyig Z.T instead of Z. So Z is not altered but it will go
out of scope just two
lines later anyway. From the picture I've provided in
#3753
one can see the axes still remain correct. But I can retry that with
the code checked out
from my github fork. It's hard to write a test as it is graphics. And
probably that's the reason
why it stayed 'under the radar' up to now.

Regards,

Florian

class GeomDensity2D(Layer):
def work(self, fig=None, ax=None):
"""Draw a two dimensional kernel density plot.
You can specify either a figure or an axis to draw on.

Parameters:

fig: matplotlib figure object
ax: matplotlib axis object to draw on

Returns:

fig, ax: matplotlib figure and axis objects
"""
if ax is None:
if fig is None:
return fig, ax
else:
ax = fig.gca()
x = self.data[self.aes['x']]
y = self.data[self.aes['y']]
rvs = np.array([x, y])
x_min = x.min()
x_max = x.max()
y_min = y.min()
y_max = y.max()
X, Y = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([x, y])
import scipy.stats as stats
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
ax.contour(Z, extent=[x_min, x_max, y_min, y_max])
return fig, ax

Am 22.7.2013 03:48, schrieb Phillip Cloud:

i see that the fix could be just transposition but are you sure that
doesn't break anything else? seemingly trivial things can break other
things in a non trivial way. in this case you're probably right, but
it's good to be sure

Reply to this email directly or view it on GitHub [1].

Links:

[1]
#3753 (comment)

@jreback
Copy link
Contributor

jreback commented Sep 28, 2013

@flo-rian doing a PR on this?

@jreback
Copy link
Contributor

jreback commented Jan 26, 2015

xref #3445

@jreback jreback closed this as completed Jan 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants