在 Seaborn 多图中调整 y 轴

Xue Xu

我正在根据我的模拟结果绘制一个 CSV 文件。该图在同一图中具有三个图形fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(24, 6))

但是,出于比较目的,我希望所有图中的 y 轴都从零开始并以特定值结束。我尝试了Seaborn 作者在此处提到的解决方案我没有收到任何错误,但该解决方案对我也不起作用。

这是我的脚本:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

fname = 'results/filename.csv'

def plot_file():
    fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(24, 6))
    df = pd.read_csv(fname, sep='\t')
    profits = \
        df.groupby(['providerId', 'periods'], as_index=False)['profits'].sum()

    # y-axis needs to start at zero and end at 10
    g = sns.lineplot(x='periods',
                     y='profits',
                     data=profits,
                     hue='providerId',
                     legend='full',
                     ax=axes[0])

    # y-axis need to start at zero and end at one
    g = sns.scatterplot(x='periods',
                        y='price',
                        hue='providerId',
                        style='providerId',
                        data=df,
                        legend=False,
                        ax=axes[1])
    # y-axis need to start at zero and end at one
    g = sns.scatterplot(x='periods',
                        y='quality',
                        hue='providerId',
                        style='providerId',
                        data=df,
                        legend=False,
                        ax=axes[2])

    g.set(ylim=(0, None))
    plt.show()

    print(g) # -> AxesSubplot(0.672059,0.11;0.227941x0.77)

结果图如下:

在此处输入图片说明

如何调整每个单独的图?

布伦丹

根据您编写代码的方式,您可以使用g.axis引用每个子图轴g.axis.set_ylim(low,high)(与链接的答案相比,不同之处在于您的图表没有绘制在 seaborn 上FacetGrid。)

使用虚拟数据和不同轴范围的示例来说明:

df = pd.DataFrame(np.random.uniform(0,10,(100,2)), columns=['a','b'])

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8,4))


g = sns.lineplot(x='a',
                 y='b',
                 data=df.sample(10),
                 ax=axes[0])
g.axes.set_ylim(0,25)

g = sns.scatterplot(x='a',
                    y='b',
                    data=df.sample(10),
                    ax=axes[1])
g.axes.set_ylim(0,3.5)

g = sns.scatterplot(x='a',
                    y='b',
                    data=df.sample(10),
                    ax=axes[2])
g.axes.set_ylim(0,0.3)

plt.tight_layout()
plt.show()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章