Seaborn能否绘制断轴图?

6

我需要绘制一个带有断裂X轴的图表(例如下面的图表),使用现有数据,我的问题是是否可以使用seaborn API来实现?

enter image description here


2
是的,请看这里:http://matplotlib.org/examples/pylab_examples/broken_axis.html - ImportanceOfBeingErnest
这对seaborn也适用吗?看起来seaborn绘图生成了绘图对象本身,我可以替换这里生成的matplotlib对象: f,(ax,ax2)= plt.subplots(2,1,sharex = True) - TPWang
1
由于seaborn完全建立在matplotlib之上,因此它肯定可以工作。具体细节取决于您的实现方式。 - ImportanceOfBeingErnest
1
这里给出的答案对我很有帮助,包括使用seaborn:https://gist.github.com/pfandzelter/0ae861f0dee1fb4fd1d11344e3f85c9e - Christoph H.
2个回答

4

虽然不太漂亮,但是可以使用。

输出

%matplotlib inline  # If you are running this in a Jupyter Notebook.
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(0, 20, 500)
y = np.sin(x)

f, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, sharey=True)
ax = sns.tsplot(time=x, data=y, ax=ax1)
ax = sns.tsplot(time=x, data=y, ax=ax2)

ax1.set_xlim(0, 6.5)
ax2.set_xlim(13.5, 20)

1
一种更紧凑的版本(也替换了已弃用的tsplot)。可以通过plt.subplots_adjust(wspace=0, hspace=0)行中的wspace参数来控制图之间的距离。保留HTML标签。

enter image description here

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


x = np.linspace(0, 20, 500)
y = np.sin(x)

f, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, sharey=True)
ax = sns.lineplot(x=x, y=y, ax=ax1)
ax = sns.lineplot(x=x, y=y, ax=ax2)

ax1.set_xlim(0, 6.5)
ax2.set_xlim(13.5, 20)

plt.subplots_adjust(wspace=0, hspace=0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接