log-log plot with seaborn jointgrid

2019-03-12 09:22发布

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

1条回答
爷、活的狠高调
2楼-- · 2019-03-12 09:50

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

查看更多
登录 后发表回答