加入收藏 | 设为首页 | 会员中心 | 我要投稿 商洛站长网 (https://www.0914zz.com/)- AI应用、CDN、边缘计算、云计算、物联网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 了解matplotlib xticks语法

发布时间:2020-09-05 22:50:41 所属栏目:Python 来源:互联网
导读:我正在读书,我遇到这个代码: import matplotlib.pyplot as pltplt.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)], [wee

我正在读书,我遇到这个代码:

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是在特定时间内的“命中”(从用户到网站)的数组.

我知道代码累积了所有的时间,以便它可以在一个星期内显示,但有人可以解释这些功能做什么?我的目标是了解这一行的所有语法:

plt.xticks([w*7*24 for w in range(10)],['week %i'%w for w in range(10)])

特别:

>什么是范围?

这是生成的:

以下是附加上下文的示例数据:

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

解决方法

为了理解范围,打开python并按顺序写入以下命令:
range(7) 
 range(4,8) 
 range(3,11,2)

对于plt.xticks中的列表解析,它们基本上是一种紧凑的写入循环方式.它们是非常常见的,有用的和整齐的.为了理解他们:

[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的简短说明.

(编辑:商洛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读