Page View Time Series Visualizer adjacent box plots

Create a draw_box_plot function that uses Seaborn to draw two adjacent box plots similar to “examples/Figure_3.png”
I am able to create individual box plots
have tried multiple ways to create 2 adjacent box plots but they always end up over lapping in the last column and row i.e. [1st row][2nd column] .

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36

Challenge: Page View Time Series Visualizer

Link to the challenge:

While it would be nice if you told us what exactly you tried so far - what you are looking for is the matplotlib.pyplot.subplots(), where you just define a grid of plots and then just have to properly fill them in.

Hi,
Thank you for your quick response
Below is the code:

METHOD 1

fig, (f1,f2) = plt.subplots(1,2)
fig.set_figwidth(20)
fig.set_figheight(10)

f1.set_title(“Year-wise Box Plot (Trend)”)
f1 = sns.boxplot(x =‘Year’, y =‘Page Views’, data = df_box )
f1.set_xlabel(“Year”)
f1.set_ylabel(“Page Views”)

f2 = sns.boxplot(x=“month”, y = “Page Views”, data = df_box)
f2.set_title(“Month-wise Box Plot (Seasonality)”)
f2.set_xlabel(“Month”)
f2.set_ylabel(“Page Views”)

refer image mtd 1 for results

METHOD 2

fig,a = plt.subplots(1,2,figsize=(16,5))

f1 = fig.add_subplot(111)
f2 = fig.add_subplot(122)

f1.set_title(“Year-wise Box Plot (Trend)”)
f1 = sns.boxplot(x =‘Year’, y =‘Page Views’, data = df_box )

f2.set_title(“Month-wise Box Plot (Seasonality)”)
f2 = sns.boxplot(x=“month”, y = “Page Views”, data = df_box)
f2.set_xlabel(“Month”)
f2.set_ylabel(“Page Views”)

refer image mtd 2 for results


I have attached both the results

Yeah the problem if you have multiple plots or “axes-objects” created you have to tell Seaborn which to use with the ax-attribute.
If you don’t, it will add the plot to the whatever the current axes is, which by default is the last one created → hence both times the plot is in the second grid.

The axes-object created in your first-code are f1 and f2 → so those have to be given to the ax-attribute.
https://seaborn.pydata.org/generated/seaborn.boxplot.html

Thank you.
I did try ax attribute and indeed were able to get the desired result.
As I am new I don’t now how valid this question would be, but what I was unable to understand with the use of ax attribute is:
while using ax the way i referenced X and Y values in the function had to be changed from
this :
sns.boxplot(x=“month”, y = “Page Views”, data = df_box)
to this :
sns.boxplot(x=df_box[“month”], y = df_box[‘Page Views’], data = df_box, ax = f2)

I am unable to understand the reason behind this.

Must be some error which stems from another source. In my solution I used X and Y like you did in the first version while also using the ax-attribute. So this is entirely possible and I cannot say why this didn’t work for you, but my guess is, you might have missed a comma or so ^^°

Thank you for the help :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.