I am reading a book and I came across this code:
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.title("Web traffic over the last month")
plt.xlabel("Time")
plt.ylabel("Hits/hour")
plt.xticks([w*7*24 for w in range(10)],
['week %i'%w for w in range(10)])
plt.autoscale(tight=True)
plt.grid()
plt.show()
For context, x
is an array of integers corresponding to an hour. y
is an array of "hits" (from users to a website) in that particular hour.
I understand that the code accumulates all the hours so that it can display them in a week, but could someone please explain what these functions do? My goal is to understand all the syntax of this line:
plt.xticks([w*7*24 for w in range(10)],
['week %i'%w for w in range(10)])
Specifically:
range
?This is what gets generated:
Here is sample data for additional context:
1 2272
2 nan
3 1386
4 1365
5 1488
6 1337
7 1883
8 2283
9 1335
10 1025
11 1139
12 1477
13 1203
14 1311
15 1299
16 1494
17 1159
18 1365
19 1272
20 1246
21 1071
22 1876
23 nan
24 1410
25 925
26 1533
27 2104
28 2113
29 1993
30 1045
pyplot. xticks() function is used to get or set the current tick locations and labels of the x-axis. It passes no arguments to return the current values without modifying them.
range is a function in python2
which makes a list for the argument given to it:
range(5) -> [0,1,2,3,4]
range(1,5) -> [1, 2, 3, 4]
in general range(lower_index, upper_index+1)
will generate a list equivalent to
[ lower_index, upper_index]
in python2
,
you can use xrange
for better performance ( as it's uses lazy evaluation, calculating when it is needed) or range
in python3
will do the work as xrange
in python2
.
now for the line:
plt.xticks([w*24*7 for w in range(10)],['week %i'%w for w in range(10)])
actually xticks
is the interval for your x axis ticks or measurement, so as your level of measurement is in hours
so it is better to tick for each hour in a week (i.e. 7 days * 24 hours
) for the week's in the data set,
and the second list comprehension put's the label's
for that one week interval( week 0, week 1 .....)
,
one point to notice is that actually the data set you have used from the book have 748 rows so approximately (748/(24*7)) = 4.45 weeks ,,
so you really can plot the graph using range(5),
the reason the output plot is scaled to week0 - week4 is because of the line
plt.autoscale(tight=True)
,
well without plt.autoscale
the plot would have shown something like this.
hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With