Plot Multiple Graphics in the Same Figure Using Python | by Gustavo Santos | Towards Data Science

If you work with Jupyter Notebooks, you know that the right side of it becomes really low populated when it comes to graphics. That happens because you usually create one graphic by cell and it does not take more than half of your screen.
However, sometimes it is necessary to plot two graphics side-by-side, not only for the sake of better space utilization, but mainly because we, as Data Scientists, frequently need to compare plots.
Let’s say you’re analyzing the distribution of trips of a Taxi company and you want to know what’s the difference in terms of distance between trips with the pickup happening in Manhattan versus Brooklyn.
Let’s load the dataset. I will use one of the sample datasets from seaborn package called Taxis. You can load it after importing seaborn.
import seaborn as sns# Load datasetdf = sns.load_dataset('taxis')
Then let’s isolate the travels initiating in Manhattan and Brooklyn.
# Brooklyn travelsdf_bklyn = df.query(' pickup_borough == "Brooklyn" ')# Manhattan Travelsdf_mhtn = df.query(' pickup_borough == "Manhattan" ')
Now it’s the time to see how the magic happens. First of all, we’re creating a figure and indicating what’s the quantity of lines and columns we want (in the example below it’s 2 plots on the same row).
Then we should just create our plots and indicate where those will go in the grid using slice notation. If you have only one row, then just one pair of square brackets is needed ( my_grid[0]). When you have multiple rows and columns, use two pairs of square brackets ( my_grid[0][0] means plot on first row , first column).
# Creating a grid figure with matplotlibfig, my_grid = plt.subplots(nrows=1, ncols=2, figsize=(18,8))# Histograms# Plot 1g1 = sns.histplot(data=df_bklyn, x='distance', ax=my_grid[0])# Title of the Plot 1g1.set_title('Histogram of the travels beginning in Brooklyn')# Plot 2g2 = sns.histplot(data=df_mhtn, x='distance', ax=my_grid[1])# Title of the Plot 2g2.set_title('Histogram of the travels beginning in Manhattan');
The multiple plots with matplotlib is pretty similar, but let’s see the little difference when coding it.
You will notice that when we create the grid, we must use tuples and lists. If we have just a single row, you can use just one tuple.
# Creating a grid figure with matplotlib SINGLE ROW EXAMPLEfig, (g1, g2, g3) = plt.subplots(nrows=1, ncols=3, figsize=(18,8))
But in case you have multiple rows, use a list of tuples, being one for each row.
# Creating a grid figure with matplotlib MULTIPLE ROWS EXAMPLEfig, [(g1, g2, g3), (g4, g5, g6)] = plt.subplots(nrows=2, ncols=3, figsize=(18,8))
Let’s see a practical example of the histograms of the distances for 4 different neighborhoods from NYC. Notice that here we do not use the ax parameter anymore.
# Creating a grid figure with matplotlibfig, [(g1, g2), (g3, g4)] = plt.subplots(nrows=2, ncols=2, figsize=(18,8))# Histograms# Plot 1g1.hist(data=df_bklyn, x='distance')# Title of the Plot 1g1.set_title('Histogram of the travels beginning in Brooklyn')# Plot 2g2.hist(data=df_mhtn, x='distance')# Title of the Plot 2g2.set_title('Histogram of the travels beginning in Manhattan')# Plot 3g3.hist(data=df_queens, x='distance')# Title of the Plot 3g3.set_title('Histogram of the travels beginning in Queens')# Plot 4g4.hist(data=df_bronx, x='distance')# Title of the Plot 4g4.set_title('Histogram of the travels beginning in Bronx');
Quick post and yet very useful. I remember having this noted in a notebook and frequently had to come back to refresh my mind how to do that.
That said, I hope this post plays the same role, helping beginners to plot multiple graphics in the same figure with ease.
Remember to follow me for more.
gustavorsantos.medium.com
If you’d like to subscribe to Medium, here’s my referral link.