在 Seaborn 图中更改轴

Change axes within Seaborn figure

我对编程比较陌生,在这里也是全新的,所以请放轻松。我在 Python 中有一个查询,即特定分支机构每周 returns 每周收入,'stops'(交付)和 'pieces'(包裹),可以追溯到几周用户请求。我想使用 Seaborn 打印一个图形,显示每个图彼此相邻,但我也希望能够编辑这些图。例如,我无法弄清楚如何将 Y 轴更改为读取 "Revenue" 而不是 "mean(Revenue)" 而不将其作为单独的图形。 Stops 和 Pieces 也一样。尝试更改各个轴上的任何内容似乎都行不通。另外,如何给图添加标题?我试过了,它似乎忽略了我的代码。

查看此处的代码及其当前返回的图像:

    customer_rev_df = pd.DataFrame(customer_rev, columns='Week Revenue Pieces Stops'.split()).tail(weeks)
    print(customer_rev_df.set_index('Week'))
    sns.set_style(style='whitegrid')
    fig, axs = plt.subplots(ncols=3, figsize=(16, 6))
    ax1 = sns.factorplot(x='Week', y='Revenue', data=customer_rev_df, ax=axs[0])
    ax2 = sns.factorplot(x='Week', y='Stops', data=customer_rev_df, ax=axs[1])
    ax3 = sns.factorplot(x='Week', y='Pieces', data=customer_rev_df, ax=axs[2])
    fig.show()

Graph as it currently looks

感谢您提供的任何帮助!

您可以使用 ax.set_ylabel()

为每个图指定不同的标签

一些示例代码:

df = pd.DataFrame({'A':range(0,5), 'B':range(0,5), 'C':range(0,5)})
sns.set_style(style='whitegrid')
fig, axs = plt.subplots(ncols=3)
ax1 = axs[0].plot(df.A.values)
ax2 = axs[1].plot(df.B.values)
ax3 = axs[2].plot(df.C.values)

axs[0].set_ylabel('Revenue')
axs[1].set_ylabel('Stops')
axs[2].set_ylabel('Pieces')

axs[0].set_title('Revenue')
axs[1].set_title('Stops')
axs[2].set_title('Pieces')

fig.show()

对于您的代码,您需要:

customer_rev_df = pd.DataFrame(customer_rev, columns='Week Revenue Pieces Stops'.split()).tail(weeks)
print(customer_rev_df.set_index('Week'))
sns.set_style(style='whitegrid')
fig, axs = plt.subplots(ncols=3, figsize=(16, 6))
ax1 = sns.factorplot(x='Week', y='Revenue', data=customer_rev_df, ax=axs[0])
ax2 = sns.factorplot(x='Week', y='Stops', data=customer_rev_df, ax=axs[1])
ax3 = sns.factorplot(x='Week', y='Pieces', data=customer_rev_df, ax=axs[2])

axs[0].set_ylabel('Revenue')
axs[1].set_ylabel('Stops')
axs[2].set_ylabel('Pieces')

axs[0].set_title('Revenue')
axs[1].set_title('Stops')
axs[2].set_title('Pieces')


fig.show()

也可以遍历标签列表,例如

labels = ['Revenue','Stops','Pieces']
for label, ax in zip(labels, axs):
    ax.set_ylabel(label)
    ax.set_title(label)