Python 了解 matplotlib xticks 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19427188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding matplotlib xticks syntax
提问by user1477388
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.
对于上下文,x
是对应于一个小时的整数数组。 y
是该特定小时内的一系列“点击”(从用户到网站)。
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:
具体来说:
- What is
range
?
- 什么是
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
采纳答案by Jblasco
In order to understand range, open python and write in sequence the following commands:
为了理解range,打开python,依次写出如下命令:
range(7)
range(4,8)
range(3,11,2)
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:
对于 plt.xticks 中的列表推导式,它们基本上是编写循环的一种紧凑方式。它们非常常见、有用且整洁。为了理解它们:
[w*2 for w in range(10)]
[w*2 for w in range(10) if w < 4]
Finally, for the command plt.xticks itself you can check http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticksfor a very brief explanation with simple examples.
最后,对于命令 plt.xticks 本身,您可以查看http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticks以获取带有简单示例的非常简短的解释。
回答by lazarus
range is a function in python2
which makes a list for the argument given to it:
range 是一个函数,python2
它为给定的参数创建一个列表:
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
,
通常range(lower_index, upper_index+1)
会生成一个等效于[ 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
.
你可以使用xrange
更好的性能(因为它的用途懒惰的评价,在需要时计算),或range
在python3
做这项工作作为xrange
中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 .....)
,
实际上xticks
是你的 x 轴刻度或测量的间隔,所以你的测量级别是在hours
所以最好在一周中的每个小时(即7 days * 24 hours
)为数据集中的一周打勾,第二个列表理解是label's
在那一周的时间间隔内( 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 ,,
需要注意的一点是,实际上您从书中使用的数据集有 748 行,因此大约 (748/(24*7)) = 4.45 周,,
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.
所以你真的可以使用 range(5) 绘制图形,输出图缩放到 week0 - week4 的原因是因为 line
plt.autoscale(tight=True)
,如果没有plt.autoscale
plot 会显示这样的东西。
hope it helps.
希望能帮助到你。