Help Fine Tuning This 2 by 2 Seaborn Subplot

Screen Link:

I am working on my guided project for “Answering Business Questions Using SQL” and am having trouble fine tuning some aesthetics for a 4 Seaborn barplot axes.

I am trying to get my Seaborn/Matplotlib code writing to be more efficient. Instead of writing code 4 times for 4 separate bar plots, I want to do it using a for loop since the 4 axes will pull from the same dataframe. So far I’ve included some common aesthetics inside the for loop such as x-tick label font size and rotation.

Outside the for loop, I’d like to specify separate aesthetics for each of the 4 axes (ax1, ax2, etc.) such as y-axis label and separate titles for each ax. This is where I am getting stuck.

  1. Is my overall method ok?
  2. Can my for loop code be improved?
  3. I’m assuming my axes should be named ax1, ax2, etc. so I can call each separately but when I call them it says name 'ax1' not defined.

I’d like have full flexibility to be able to change any aesthetic parameter on any ax.

I’ve uploaded a Jupyter notebook with only the relevant part included, screenshots, and this code of the for loop:

fig, axes = plt.subplots(2,2, figsize=(16,16))

list1 = ['sales', 'avg_sales_per_customer', 'avg_sales_per_order', 'customers']

for x, ax in zip(list1, axes.flatten()):          
    sns.barplot(x=country_list, y=x, data=country_stats, ax=ax, palette=("Blues"))
    ax.set_xticklabels(country_list,rotation=45, ha='right')
    ax.tick_params(labelsize=12)
    sns.despine()
ax1.set_ylabel('Sales By Country')
ax2.set_ylabel('Sales Per Customer')

Thank you for your time!

Chinook Sample for help with plots.ipynb (94.2 KB)

Click here to view the jupyter notebook file in a new tab

Hi @gosaints

I understand this is a very delayed response. still…

Have you been able to solve your query on your own or you would prefer to have some inputs?

1 Like

Hi @Rucha,
Thank you for responding! Sure, I’d love to hear your thoughts. I’ve uploaded my finished project here if you’d like to take a look. It is in cell 6 of the notebook.

I ended up just manually writing out the 4 plots: creating the figure and axes via plt.subplots() and then assigning each ax via plt.subplot().

I was able to achieve what I had in mind but I am always interested in better and more efficient ways of doing things.

Thank you so much for your time!

SQL Chinook Record Store DB.ipynb (1.9 MB)

Click here to view the jupyter notebook file in a new tab

Hey @gosaints

Your plots are amazing. :+1: Haven’t gone through the project technically so I can’t comment on the data work, but I really liked the structure and flow. Cool work here. :smiley:

I have added few cells with the title of the post. This might be the most basic method to work with sub axes in a for loop.

I had searched Stack Over Flow and found lot of helpful posts to work with subplots. (and I am still learning too!). There are several posts at DQ community as well. You should check out them as well. The other students have also come up with great hacks to work with subplots.

I only provided the codes with simple one-line comments. Basically experiment and manipulate the codes based on your research. In case you need some clarity on anything, let me know.

All the best and happy learning :slight_smile:

SQL Chinook Record Store DB.ipynb (2.6 MB)

Click here to view the jupyter notebook file in a new tab

1 Like

Thank you @rucha, this is very helpful! I appreciate you taking the time to upload this

1 Like