Emac
Emac

Reputation: 1167

Change axes within Seaborn figure

I'm relatively new to programming and brand new here, so go easy on me. I have a query within Python that returns weekly revenue, 'stops' (deliveries), and 'pieces' (packages) per week for a particular branch, going back as many weeks as the user requests. I want to print a figure using Seaborn that shows each plot next to each other, but I also want to be able to edit the plots. For instance, I can't figure out how to change the Y axis to read "Revenue" instead of "mean(Revenue)" without making it a separate figure. Same for Stops and Pieces. Trying to change anything on the individual axes doesn't seem to work. Also, how do I add a title to the figure? I have tried and it just seems to ignore my code.

See the code here and the image of what it is currently returning:

    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

Thanks for any help you can provide!

Upvotes: 1

Views: 1590

Answers (1)

Chuck
Chuck

Reputation: 3852

You can specify a different label for each plot using ax.set_ylabel()

For some sample code:

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

enter image description here

For your code, you will want:

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

Can also iterate over a list of labels e.g.

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

Upvotes: 3

Related Questions