简体   繁体   中英

Use Seaborn to plot 1D time series as a line with marginal histogram along y-axis

I'm trying to recreate the broad features of the following figure:

在此输入图像描述

(from EM Ozbudak, M. Thattai, I. Kurtser, AD Grossman, and A. van Oudenaarden, Nat Genet 31, 69 (2002))

seaborn.jointplot does most of what I need, but it seemingly can't use a line plot, and there's no obvious way to hide the histogram along the x-axis. Is there a way to get jointplot to do what I need? Barring that, is there some other reasonably simple way to create this kind of plot using Seaborn?

You can use ax_marg_x.patches to affect the outcome.

Here, I use it to turn the x-axis plot white so that it cannot be seen (although the margin for it remains):

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="white", color_codes=True)

x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0,  0.5]], 1000).T
g = sns.jointplot(x=x, y=y, kind="hex", stat_func=None, marginal_kws={'color': 'green'})
plt.setp(g.ax_marg_x.patches, color="w", )

plt.show()

Output:

Seaborn Plot

Here is a way to create roughly the same plot as shown in the question. You can share the axes between the two subplots and make the width-ratio asymmetric.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)

x = np.linspace(0,8, 300)
y = np.tanh(x)+np.random.randn(len(x))*0.08

fig, (ax, axhist) = plt.subplots(ncols=2, sharey=True,
                                 gridspec_kw={"width_ratios" : [3,1], "wspace" : 0})

ax.plot(x,y, color="k")
ax.plot(x,np.tanh(x), color="k")

axhist.hist(y, bins=32, ec="k", fc="none", orientation="horizontal")
axhist.tick_params(axis="y", left=False)

plt.show()

在此输入图像描述

It turns out that you can produce a modified jointplot with the needed characteristics by working directly with the underlying JointGrid object:

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

x = np.linspace(0,8, 300)
y = (1 - np.exp(-x*5))*.5
ynoise= y + np.random.randn(len(x))*0.08

grid = sns.JointGrid(x, ynoise, ratio=3)
grid.plot_joint(plt.plot)
grid.ax_joint.plot(x, y, c='C0')

plt.sca(grid.ax_marg_y)
sns.distplot(grid.y, kde=False, vertical=True)

# override a bunch of the default JointGrid style options
grid.fig.set_size_inches(10,6)
grid.ax_marg_x.remove()
grid.ax_joint.spines['top'].set_visible(True)

Output:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM