准备工作
[root@localhost Python-3.5.0]# source /py3env/bin/activate
(py3env) [root@localhost WeatherData]# pip install numpy
(py3env) [root@localhost WeatherData]# pip install scikit-learn
(py3env) [root@localhost WeatherData]# pip install matplotlib
(py3env) [root@localhost WeatherData]# pip install datetime
(py3env) [root@localhost WeatherData]# pip install pandas
(py3env) [root@localhost WeatherData]# pip install matplotlib
(py3env) [root@localhost WeatherData]# vim pic-1.py
让vim每次tab刚好为四个空格
(py3env) [root@localhost WeatherData]# vim /etc/vimrc
set ts=4
set expandtab
set autoindent
对于tk找不到错
ImportError: No module named ‘_tkinter’
python3.5.2安装tkinter
yum -y install tkinter tcl-devel tk-devel
vim ......./Python3.5.2/Modules/Setup.dist 中去掉如下行的注释
_tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \
-L/usr/local/lib \
-I/usr/local/include \
-ltk8.5 -ltcl8.5 \ #rpm -qa |grep tcl查看版本
-lX11
再编译安装Python
./configure
#make && make install
从两个文件中获取数据生成曲线图
(py3env) [root@localhost WeatherData]# cat test1.csv
,temp,humidity,pressure,description,dt,wind_speed,wind_deg,city,day,dist
0,24.689999999999998,60,1017,Sky is Clear,1435390925,2.6,140,test1,2015-06-27 09:42:05,250
1,25.340000000000032,57,1017,Sky is Clear,1435394243,2.1,160,test1,2015-06-27 10:37:23,250
2,27.700000000000045,51,1017,Sky is Clear,1435399015,1.5,210,test1,2015-06-27 11:56:55,250
3,28.360000000000014,42,1017,Sky is Clear,1435402416,2.1,220,test1,2015-06-27 12:53:36,250
4,29.450000000000045,42,1016,few clouds,1435406054,2.6,210,test1,2015-06-27 13:54:14,250
5,30.27000000000004,39,1015,few clouds,1435409700,2.1,230,test1,2015-06-27 14:55:00,250
6,30.510000000000048,42,1014,Sky is Clear,1435416893,2.6,250,test1,2015-06-27 16:54:53,250
7,30.600000000000023,40,1014,Sky is Clear,1435420537,3.1,220,test1,2015-06-27 17:55:37,250
8,30.180000000000007,35,1013,Sky is Clear,1435424291,2.6,270,test1,2015-06-27 18:58:11,250
9,28.950000000000045,35,1013,heavy intensity rain,1435427929,1.5,310,test1,2015-06-27 19:58:49,250
10,23.879999999999995,60,1015,Sky is Clear,1435438353,2.1,200,test1,2015-06-27 22:52:33,250
11,22.99000000000001,57,1015,Sky is Clear,1435442236,2.1,210,test1,2015-06-27 23:57:16,250
12,21.950000000000045,56,1016,few clouds,1435445858,4.6,120,test1,2015-06-28 00:57:38,250
13,20.27000000000004,68,1017,Sky is Clear,1435453226,2.6,90,test1,2015-06-28 03:00:26,250
14,19.620000000000005,72,1017,Sky is Clear,1435456482,2.6,50,test1,2015-06-28 03:54:42,250
15,18.680000000000007,72,1017,Sky is Clear,1435460036,2.6,40,test1,2015-06-28 04:53:56,250
16,18.28000000000003,72,1017,Sky is Clear,1435463874,2.6,40,test1,2015-06-28 05:57:54,250
17,18.860000000000014,77,1017,Sky is Clear,1435467177,0.5,0,test1,2015-06-28 06:52:57,250
(py3env) [root@localhost WeatherData]# cat test2.csv
,temp,humidity,pressure,description,dt,wind_speed,wind_deg,city,day,dist
0,22.57000000000005,67,1013,Sky is Clear,1435387607,1.03,279,test2,2015-06-27 08:46:47,200
1,24.610000000000014,60,1017,Sky is Clear,1435390790,2.6,140,test2,2015-06-27 09:39:50,200
2,25.480000000000018,57,1017,Sky is Clear,1435394201,2.1,160,test2,2015-06-27 10:36:41,200
3,26.52000000000004,53,1012,Sky is Clear,1435398622,1.54,0,test2,2015-06-27 11:50:22,200
4,28.0,49,1012,sky is clear,1435402050,1.03,230,test2,2015-06-27 12:47:30,200
5,28.970000000000027,42,1011,sky is clear,1435405691,3.08,305,test2,2015-06-27 13:48:11,200
6,29.720000000000027,40,1011,sky is clear,1435409355,2.06,316,test2,2015-06-27 14:49:15,200
7,30.890000000000043,44,1009,scattered clouds,1435416560,1.54,347,test2,2015-06-27 16:49:20,200
8,30.129999999999995,46,1008,scattered clouds,1435420160,2.5700000000000003,280,test2,2015-06-27 17:49:20,200
9,30.0,46,1008,Sky is Clear,1435423882,1.54,283,test2,2015-06-27 18:51:22,200
10,30.120000000000005,47,1008,Sky is Clear,1435427527,3.08,311,test2,2015-06-27 19:52:07,200
11,25.30000000000001,54,1010,Sky is Clear,1435438046,1.54,36,test2,2015-06-27 22:47:26,200
12,18.879999999999995,62,1011,Sky is Clear,1435441821,5.65,58,test2,2015-06-27 23:50:21,200
13,22.29000000000002,67,1011,Sky is Clear,1435445468,3.6,41,test2,2015-06-28 00:51:08,200
14,20.510000000000048,73,1012,Sky is Clear,1435452811,5.14,59,test2,2015-06-28 02:53:31,200
15,19.629999999999995,75,1012,Sky is Clear,1435456156,2.06,3,test2,2015-06-28 03:49:16,200
16,18.910000000000025,80,1012,Sky is Clear,1435459664,1.54,48,test2,2015-06-28 04:47:44,200
17,19.110000000000014,82,1012,Sky is Clear,1435463430,0.89,226.003,test2,2015-06-28 05:50:30,200
18,18.680000000000007,81,1013,Sky is Clear,1435466829,0.51,178,test2,2015-06-28 06:47:09,200
19,20.77000000000004,72,1013,Sky is Clear,1435470506,1.06,138.501,test2,2015-06-28 07:48:26,200
(py3env) [root@localhost WeatherData]# cat pic1.py
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import datetime
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil import parser
# 读取CSV数据
df_test1 = pd.read_csv('test1.csv')
df_test2= pd.read_csv('test2.csv')
# 取出我们要分析的温度和日期数据
y1 = df_test1['temp']
x1 = df_test1['day']
y2 = df_test2['temp']
x2 = df_test2['day']
# 把日期数据转换成 datatime 格式
day_test1 = [parser.parse(x) for x in x1]
day_test2 = [parser.parse(x) for x in x2]
# 调用 subplot 函数, fig 是图像对象,ax 是坐标轴对象
fig ,ax = plt.subplots()
# 调整x轴坐标刻度,使其旋转70度,方便查看
plt.xticks(rotation=70)
# 设定时间的格式
hours = mdates.DateFormatter('%H:%M')
# 设定X轴显示的格式
ax.xaxis.set_major_formatter(hours)
# 画多条, day_test1是X轴数据,y1是Y轴数据,‘r’代表的是'red' 红色, linestyle是线条样式, marker是数据标识, label是线条对应显示名称
ax.plot(day_test1, y1, 'r', linestyle='-', marker='o', label='y1 Data')
ax.plot(day_test2, y2, 'b', linestyle='--', label='y2 Data')
# 将线条label放置到左上角
plt.legend(loc='upper left')
# 设置图片标题
plt.title('Plot 1', size=14)
# 设置x轴标题
plt.xlabel('x-axis', size=14)
# 设置y轴标题
plt.ylabel('y-axis', size=14)
# 画图
plt.savefig('/plot1.png', format='png')
根据数组数据生成饼状图
(py3env) [root@localhost WeatherData]# vim pic2.py
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
data = [33, 25, 20, 12, 10]
# 创建一个宽6英寸、高6英寸的图(赋值1)
plt.figure(num=1, figsize=(6, 6))
# 添加一个长宽比为1的轴图
plt.axes(aspect=1)
#i 设置图的标题(字号为14)
plt.title('Plot 3', size=14)
# 用data列表画一个包含标签的饼状图
plt.pie(data, labels=('Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5'))
plt.savefig('/plot2.png', format='png')
根据随机生成的100个数据生成柱状分布图
(py3env) [root@localhost WeatherData]# vim pic3.py
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import datetime
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil import parser
mu = 0.0
sigma = 2.0
# 创建一个包含100个随机样本的正态分布数据集
samples = np.random.normal(loc=mu, scale=sigma, size=100)
# 创建一个宽8英寸、高6英寸的图(赋值1)
plt.figure(num=1, figsize=(8, 6))
# 设置标题
plt.title('Plot 2', size=14)
# 设置x轴显示名称与y轴显示名称
plt.xlabel('value', size=14)
plt.ylabel('counts', size=14)
# 以生成的100分布数据集为x轴,画出分布图
plt.hist(samples, bins=40)
plt.savefig('/plot3.png', format='png')
执行python时报以下错
_tkinter.TclError: no display name and no $DISPLAY environment variable
需要在代码import matplotlib后添加matplotlib.use('Agg')