Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log-log plot with seaborn jointgrid

I'm trying to create a loglog plot with a KDE and histogram associated with each axis using a seaborn JointGrid object. This gets me pretty close, but the histogram bins do not translate well into logspace. Is there a way to do this easily without having to recreate the marginal axes?

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

data = sns.load_dataset('tips')
g = sns.JointGrid('total_bill', 'tip', data)
g.plot_marginals(sns.distplot, hist=True, kde=True, color='blue')
g.plot_joint(plt.scatter, color='black', edgecolor='black')
ax = g.ax_joint
ax.set_xscale('log')
ax.set_yscale('log')
g.ax_marg_x.set_xscale('log')
g.ax_marg_y.set_yscale('log')

Output of plot

like image 626
Koppology Avatar asked Sep 26 '14 12:09

Koppology


People also ask

How do you use the log scale in Seaborn?

To make the x-axis to log scale, we first the make the scatter plot with Seaborn and save it to a variable and then use set function to specify 'xscale=log'. We see a linear pattern between lifeExp and gdpPercap. Now, the scatter plot makes more sense.

What is Joinplot in Seaborn?

countplot() method is used to Show the counts of observations in each categorical bin using bars. Syntax : seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

How do you plot a Jointplot?

A Jointplot comprises three plots. Out of the three, one plot displays a bivariate graph which shows how the dependent variable(Y) varies with the independent variable(X). Another plot is placed horizontally at the top of the bivariate graph and it shows the distribution of the independent variable(X).

What is joint grid plot in Seaborn?

Grid for drawing a bivariate plot with marginal univariate plots. Many plots can be drawn by using the figure-level interface jointplot() . Use this class directly when you need more flexibility.


1 Answers

For log histograms I find generally useful to set your own bins with np.logspace().

mybins=np.logspace(0,np.log(100),100)

Then just set bins= in _marginals

data = sns.load_dataset('tips')
g = sns.JointGrid('total_bill', 'tip', data,xlim=[1,100],ylim=[0.01,100])
g.plot_marginals(sns.distplot, hist=True, kde=True, color='blue',bins=mybins)
g.plot_joint(plt.scatter, color='black', edgecolor='black')
ax = g.ax_joint
ax.set_xscale('log')
ax.set_yscale('log')
g.ax_marg_x.set_xscale('log')
g.ax_marg_y.set_yscale('log')

enter image description here

like image 86
lalmei Avatar answered Sep 23 '22 02:09

lalmei