I need to draw a broken x axis graph (e.g. the graph below) with existing data, my question is whether it's possible to use seaborn APIs to do that?
Asked
Active
Viewed 1.1k times
6
-
2Yes, look here: http://matplotlib.org/examples/pylab_examples/broken_axis.html – ImportanceOfBeingErnest Mar 24 '17 at 18:05
-
Does this work for seaborn as well? It looks like seaborn plot generates the plot object itself, and I can replace the matplotlib object generated here: `f, (ax, ax2) = plt.subplots(2, 1, sharex=True)` – TPWang Mar 24 '17 at 18:23
-
1As seaborn is completely built on top of matplotlib it will surely work. The details depend on your implementation. – ImportanceOfBeingErnest Mar 24 '17 at 18:28
-
1The answer given here worked nicely for me and includes the use of seaborn: https://gist.github.com/pfandzelter/0ae861f0dee1fb4fd1d11344e3f85c9e – Christoph H. Mar 01 '21 at 17:44
2 Answers
4
Not as pretty as I'd like but works.
%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)

Unapiedra
- 15,037
- 12
- 64
- 93
1
A tighter version (also replaced the deprecated tsplot
). Can control the distance between the plots by the wspace
parameter in the plt.subplots_adjust(wspace=0, hspace=0)
line.
%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)

nocibambi
- 2,065
- 1
- 16
- 22