问题描述
我正在看一本书,我遇到了这个代码:
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()
对于上下文, x
是一个整数数组,对应于一个小时.y
是该特定小时内的点击"数组(从用户到网站).
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)])
特别是:
- 什么是
range
?
这是生成的:
以下是附加上下文的示例数据:
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
推荐答案
为了理解range,打开python,依次编写如下命令:
In order to understand range, open python and write in sequence the following commands:
range(7)
range(4,8)
range(3,11,2)
对于plt.xticks中的列表理解,它们基本上是一种编写循环的紧凑方式.它们非常常见,有用且简洁.为了理解它们:
For the list comprehensions within the plt.xticks, they are basically a compact way of writing loops. They are very common, useful and neat. In order to understand them:
[w*2 for w in range(10)]
[w*2 for w in range(10) if w < 4]
最后,对于命令plt.xticks本身,您可以检查 http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticks ,其中包含简单示例的简短说明.
Finally, for the command plt.xticks itself you can check http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticks for a very brief explanation with simple examples.
这篇关于了解 matplotlib xticks 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!