FAQ: Why Data Science? - Visualizing Data with Matplotlib and Seaborn

This community-built FAQ covers the “Visualizing Data with Matplotlib and Seaborn” exercise from the lesson “Why Data Science?”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

FAQs on the exercise Visualizing Data with Matplotlib and Seaborn

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

plt.show() does nothing… I tried in Chrome and Firefox, but in neither one does a plot appear although the instruction checkmark goes green as though everything worked.

What does the following code do?
y_upper = [i + (i*0.15) for i in viewers_hour]

I think there’s a bug in the browser. The code doesn’t actually do anything so nothing should print out. If you click to the next exercise it should show the plotting code in full.

sets variable i to viewers_hour and does a mathematical function on it. Essentially with the plt.fill_between code draws in the shading around the line

viewers_hour = [30, 17, 34, 29, 19, 14, 3, 2, 4, 9, 5, 48, 62, 58, 40, 51, 69, 55, 76, 81, 102, 120, 71, 63]

why the values are set in that order?

2 Likes

also curious about this

What is the code ax = plt.subplot() doing here? I tried removing it and running the code but I did not see anything change.

Wondering about line 8 of the code, as other respondents have noted, where do these numbers come from, and why are they in this order?

viewers_hour = [30, 17, 34, 29, 19, 14, 3, 2, 4, 9, 5, 48, 62, 58, 40, 51, 69, 55, 76, 81, 102, 120, 71, 63]

Is this something we will learn in a future lesson?

So this is my analysis, feel free to ask me if somethings not clear:

hour = range(24)
#hour is declared as a Python range of consecutive numbers, from 0 to 23 (remember 24 digits starting from 0 so the highest is 23)

viewers_hour = [30, 17, 34, 29, 19, 14, 3, 2, 4, 9, 5, 48, 62, 58, 40, 51, 69, 55, 76, 81, 102, 120, 71, 63]
#viewers_hour is a list of made-up numbers, they represent an example of the total hours spent by Code Academy users on the platform on a full day, see how the list is made up of 24 numbers?

plt.title(“Codecademy Learners Time Series”)
#declaring a title for the graph

plt.xlabel(“Hour”)
#naming the x axis
plt.ylabel(“Viewers”)
#naming the y axis

plt.plot(hour, viewers_hour)
#creating the graph, the values of hour are the X and the values of viewers_hour are the Y

plt.legend([‘2015-01-01’])
#the graphs legend

ax = plt.subplot()
#now this was curious, seems that a subplot (which allows us to draw another graph on top of our current one) is necessary for:

ax.set_facecolor(‘seashell’)
#set the graph’s yellowish background color
ax.set_xticks(hour)
#make it so that all 24 values in hour show up in the X axis
ax.set_yticks([0, 20, 40, 60, 80, 100, 120])
#define the values of the ticks on Y

#interestingly I tried doing this on the original graph and it didn’t work! like this:
#plt.set_xticks(hour)
#plt.set_yticks([0, 20, 40, 60, 80, 100, 120])
#plt.set_facecolor(‘seashell’)
#why did this not work? I have no idea!

#That’s it for the use of the ax subplot!

y_upper = [i + (i*0.15) for i in viewers_hour]
#this creates a new viewers_hour list but the values are slightly larger

y_lower = [i - (i*0.15) for i in viewers_hour]
#this creates a new viewers_hour list but the values are slightly smaller

plt.fill_between(hour, y_lower, y_upper, alpha=0.2)
#this adds color to the gap between the slightly larger and smaller values

plt.show()
#this shows the graph
#funnily enough ax.show() works identically