【问题标题】:Is it possible to draw a broken axis graph with seaborn?是否可以用seaborn绘制断轴图?
【发布时间】:2025-12-14 21:55:01
【问题描述】:

我需要用现有数据绘制一个断开的 x 轴图(例如下图),我的问题是是否可以使用 seaborn API 来做到这一点?

【问题讨论】:

标签: python matplotlib plot axis seaborn


【解决方案1】:

没有我想要的那么漂亮,但可以。

%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)

【讨论】:

    【解决方案2】:

    更紧凑的版本(也替换了已弃用的 tsplot)。可以通过plt.subplots_adjust(wspace=0, hspace=0)行中的wspace参数控制地块之间的距离。

    %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)
    

    【讨论】:

      最近更新 更多