Seaborn:我只想要对数刻度

Cal*_*arJ 9 python data-visualization pandas seaborn

我正在使用seaborn绘制一些生物学数据。

我只想分配一个基因与另一个基因(约300位患者中有表达),并且在 graph = sns.jointplot(x='Gene1',y='Gene2',data=data,kind='reg')

我喜欢该图为我提供了很好的线性拟合以及PearsonR和P值。

我的图之一。

我想要的只是将数据绘制成对数刻度,这通常是表示此类基因数据的方式。

我在网上看了一些解决方案,但是它们都摆脱了我的PearsonR值或线性拟合,或者看起来不那么好。我对此并不陌生,但似乎在对数刻度上绘制图形应该不会带来太多麻烦。

有任何意见或解决方案吗?

谢谢!

编辑:为了回应评论,我离答案越来越近了。我现在有一个绘图(如下所示),但是我需要一条拟合线并进行一些统计。现在就着手解决这个问题,但是与此同时,任何答案/建议都值得欢迎。

进展

Cal*_*arJ 8

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

g = sns.JointGrid(data1, data2, data, xlim=[.5, 1000000],
                  ylim=[.1, 10000000])
g.plot_marginals(sns.distplot, color='blue', bins=mybins)
g = g.plot(sns.regplot, sns.distplot)
g = g.annotate(stats.pearsonr)

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')
Run Code Online (Sandbox Code Playgroud)

这工作得很好。最后,我决定只将表值转换为log(x),因为这使图形在短期内更易于缩放和可视化。


cot*_*ail 4

要对图进行对数缩放,另一种方法是将参数传递给1log_scale的边际分量,这可以通过参数来完成。jointplotmarginal_kws=

import seaborn as sns
from scipy import stats
data = sns.load_dataset('tips')[['tip', 'total_bill']]**3
graph = sns.jointplot(x='tip', y='total_bill', data=data, kind='reg', marginal_kws={'log_scale': True})
#                                                                     ^^^^^^^^^^^^^ here
pearsonr, p = stats.pearsonr(data['tip'], data['total_bill'])
graph.ax_joint.annotate(f'pearsonr = {pearsonr:.2f}; p = {p:.0E}', xy=(35, 50));
Run Code Online (Sandbox Code Playgroud)

结果1a

如果我们不对轴进行对数缩放,我们会得到以下图:2 结果1b

请注意,相关系数相同,因为用于导出两条拟合线的基础回归函数相同。


尽管拟合线在上面的第一个图中看起来不是线性的,但它确实是线性的,只是轴是对数缩放的,这“扭曲”了视图。在幕后,sns.jointplot()调用sns.regplot()绘制散点图和拟合线,因此如果我们使用相同的数据调用它并对轴进行对数缩放,我们将得到相同的图。换句话说,以下将产生相同的散点图。

sns.regplot(x='tip', y='total_bill', data=data).set(xscale='log', yscale='log');
Run Code Online (Sandbox Code Playgroud)

如果您在将数据传递给 之前对其进行记录jointplot(),那么这将完全是一个不同的模型(您可能不想要它),因为现在回归系数将来自log(y)=a+b*log(x),而不是y=a+b*x像以前那样。

您可以在下图中看到差异。尽管拟合线现在看起来是线性的,但相关系数现在不同了。

结果2


1边际图是使用 绘制的sns.histplot,它承认了这一log_scale论点。

2在本文中绘制图表的便捷函数:

from scipy import stats
def plot_jointplot(x, y, data, xy=(0.4, 0.1), marginal_kws=None, figsize=(6,4)):
    # compute pearsonr
    pearsonr, p = stats.pearsonr(data[x], data[y])
    # plot joint plot
    graph = sns.jointplot(x=x, y=y, data=data, kind='reg', marginal_kws=marginal_kws)
    # annotate the pearson r results
    graph.ax_joint.annotate(f'pearsonr = {pearsonr:.2f}; p = {p:.0E}', xy=xy);
    # set figsize
    graph.figure.set_size_inches(figsize);
    return graph

data = sns.load_dataset('tips')[['tip', 'total_bill']]**3
plot_jointplot('tip', 'total_bill', data, (50, 35), {'log_scale': True})  # log-scaled
plot_jointplot('tip', 'total_bill', data, (550, 3.5))                     # linear-scaled
plot_jointplot('tip', 'total_bill', np.log(data), (3.5, 3.5))             # linear-scaled on log data
Run Code Online (Sandbox Code Playgroud)