~day fifteen
安装mataplotlib
linux:install python3-matpoltlib
pip install --user matplotlib
windows:
15.1.3 在Windows系统中安装matplotlib在Windows系统中,首先需要安装Visual Studio。为此,请访问https://dev.windows.com/,单击Downloads,再查找Visual Studio Community——一组免费的Windows开发工具。请下载并运行该安装程序。接下来,需要下载matplotlib安装程序。为此,请访问https://pypi.python.org/pypi/matplotlib/,并查找与你使用的Python版本匹配的wheel文件(扩展名为.whl的文件)。例如,如果你使用的是32位的Python 3.5,则需要下载matplotlib-1.4.3-cp35-none-win32.whl。注意 如果找不到与你安装的Python版本匹配的文件,请去http://www.lfd.uci.edu/-gohlke/pythonlibs/#matplotlib看看,这个网站发布安装程序的时间通常比matplotlib官网早些。将这个.whl文件复制到你的项目文件夹,打开一个命令窗口,并切换到该项目文件夹,再使用pip来安装matplotlib:

绘制简单的折线图——plot()

#首先导入了模块pyplot,并给它指定了别名plt,以免反复输入pyplot,pyplot包含很多用于生成图表的函数
#创建列表传递给函数plot(),这个函数尝试根据这些数字绘制出有意义的图形,plt.show()打开matplotlib查看器,并显示绘制的图形。
修改图表线条大小及标题:

#参数linewidth(见❶)决定了plot()绘制的线条的粗细
#函数title()(见❷)给图表指定标题。在上述代码中,出现了多次的参数fontsize指定了图表中文字的大小。
#函数xlabel()和ylabel()让你能够为每条轴设置标题(见❸);
#而函数tick_params()设置刻度的样式(见❹),其中指定的实参将影响x轴和y轴上的刻度(axis='both'),并将刻度标记的字号设置为14(labelsize=14)。

#指定x轴数据,向plot()提供数字,它默认其数据点对应的x坐标值为0,可用相应的列表值给x轴赋予数据。
scatter()绘制散点图


~day sixteen

#数axis()指定了每个坐标轴的取值范围(见❸)。函数axis()要求提供四个值:x和y坐标轴的最小值和最大值。
#plt.scatter(255, 255, edgecolor='none', s=20)
edgecolor='none'删除数据点的轮廓,相应调用修改为上述代码后,如果再运行scatter_squares.py,在图表中看到的将是蓝色实心点。
#matplotlib允许你给散点图中的各个点指定颜色。默认为蓝色点和黑色轮廓,在散点图包含的数据点不多时效果很好。
自定义颜色,使用参数c
#plt.scatter(255, 255, edgecolor='none',c='red', s=20)

#使用颜色映射,从起始颜色渐变到结束颜色。在可视化中,颜色映射用于突出数据的规律,例如,你可能用较浅的颜色来显示较小的值,并使用较深的颜色来显示较大的值。

#参数c设置成了一个y值列表,并使用参数cmap告诉pyplot使用哪个颜色映射。这些代码将y值较小的点显示为浅蓝色,并将y值较大的点显示为深蓝色。
#要了解pyplot中所有的颜色映射,请访问http://matplotlib.org/,单击Examples,向下滚动到Color Examples,再单击colormaps_reference。


随机漫步
RandomWalk类只包含两个方法:__init__()和fill_walk(),其中后者计算随机漫步经过的所有点。

#choice()随机决策,我们将所有可能的选择都存储在一个列表中,并在每次做决策时都使用choice()来决定使用哪种选择(见❶)。在方法__init__中国描写的方法的参数,需要重新赋值。
#在❸处,我们创建了两个用于存储x和y值的列表,并让每次漫步都从点(0, 0)出发。

#创建循环,直到达到需要的点数目。
x_direction = choice([1,-1])
x_distance = choice([0, 1, 2, 3, 4])
x_step = x_direction*x_distance
#沿x轴方向所走的方向与距离
#我们使用choice([1, -1])给x_direction选择一个值,结果要么是表示向右走的1,要么是表示向左走的-1(见❷)。接下来,choice([0, 1, 2, 3, 4])随机地选择一个0~4之间的整数,告诉Python沿指定的方向走多远(x_distance)。(通过包含0,我们不仅能够沿两个轴移动,还能够沿y轴移动。)
#如果y_step为正,就意味着向上移动,为负意味着向下移动,而为零意味着水平移动。如果x_step和y_step都为零,则意味着原地踏步,我们拒绝这样的情况,接着执行下一次循环(见❺)。
将x_values的最后的值与新增的值相加,即为下一个点的x轴的位置,再将最新的位置加入到列表x_values[ ]的末尾。

~day seventeen

#隐藏坐标轴:将看到一系列图形,看不到坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)

调整尺寸以适合屏幕
#形参figsize指定一个元组,向matplotlib指出绘图窗口的尺寸,单位为英寸。Python假定屏幕分辨率为80像素/英寸,如果上述代码指定的图表尺寸不合适,可根据需要调整其中的数字。

~day eighteen
使用pygal模拟掷骰子
安装pygal

#你可能需要使用命令pip3而不是pip,如果这还是不管用,你可能需要删除标志--user。
pygal画廊
#要了解使用Pygal可创建什么样的图表,请查看图表类型画廊:访问http://www.pygal.org/,单击Documentation,再单击Chart types。每个示例都包含源代码,让你知道这些图表是如何生成的。
#创建一个掷骰子的类


randint(1, 6)计数,是计算1~6,包括6
range(1, 6)技术,计算1~5,不包括6
#用pygal绘制直方图

#add()方法,传递值的标签与值的大小
#用rander_to_file()方法保存文件

~day nineteen

导入csv模块
#调用csv.reader(),并将前面所储存的文件对象(f)作为实参传递给它,创建一个与该文件相关联的阅读器(reader)对象。
#函数next(),调用它并将阅读器对象传递给它时,它将返回文件中的下一行。




#阅读器对象从其停留的地方继续往下读取CSV文件,每次都自动返回当前所处位置的下一行。
#每次执行该循环时,我们都将索引1处(第2列)的数据附加到highs末尾(见❸)。
#matplotlib仅读取数字数据,我们用int(row【1】)转换数据来制作图表
~day twenty

#添加日期
模块datetime中的datetime类:方法:strptime():
#将包含所需日期的字符串作为第一个实参。第二个实参告诉Python如何设置日期的格式。在这个示例中,'%Y-'让Python将字符串中第一个连字符前面的部分视为四位的年份;'%m-'让Python将第二个连字符前面的部分视为表示月份的数字;而'%d'让Python将字符串的最后一部分视为月份中的一天(1~31)。



#1.连续做多个空列表
#2.从第0列提取时间参数
#3.调用了fig.autofmt_xdate()来绘制斜的日期标签,以免它们彼此重叠。
#给图表区域着色:方法fill_between(),它接受一个x值系列和两个y值系列,并填充两个y值系列之间的空间

#实参alpha:实参alpha指定颜色的透明度。Alpha值为0表示完全透明,1(默认设置)表示完全不透明。

若其中一天的数据缺失,则Python无法处理其中一天的最高气温,因为它无法将空字符串('')转换为整数。(例:death_valley_2014.csv)
网友评论