Python基础学习14
2019-02-07 本文已影响0人
ericblue
matplotlib库安装
~/Python ⮀ pip3 install matplotlib
Collecting matplotlib
Downloading https://files.pythonhosted.org/packages/28/6c/addb3560777f454b1d56f0020f89e901eaf68a62593d4795e38ddf24bbd6/matplotlib-3.0.2-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (14.1MB)
100% |████████████████████████████████| 14.1MB 61kB/s
Requirement already satisfied: python-dateutil>=2.1 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from matplotlib) (2.7.5)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)
Downloading https://files.pythonhosted.org/packages/de/0a/001be530836743d8be6c2d85069f46fecf84ac6c18c7f5fb8125ee11d854/pyparsing-2.3.1-py2.py3-none-any.whl (61kB)
100% |████████████████████████████████| 71kB 98kB/s
Collecting kiwisolver>=1.0.1 (from matplotlib)
Downloading https://files.pythonhosted.org/packages/fb/96/619db9bf08f652790fa9f3c3884a67dc43da4bdaa185a5aa2117eb4651e1/kiwisolver-1.0.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (108kB)
100% |████████████████████████████████| 112kB 70kB/s
Collecting cycler>=0.10 (from matplotlib)
Downloading https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Requirement already satisfied: numpy>=1.10.0 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from matplotlib) (1.15.4)
Requirement already satisfied: six>=1.5 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from python-dateutil>=2.1->matplotlib) (1.11.0)
Requirement already satisfied: setuptools in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from kiwisolver>=1.0.1->matplotlib) (40.4.1)
Installing collected packages: pyparsing, kiwisolver, cycler, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.0.1 matplotlib-3.0.2 pyparsing-2.3.1
画图事例
import matplotlib.pyplot as plt
#绘制简单的曲线
plt.plot([1, 3, 5], [4, 8, 10])
plt.show()
image.png
import matplotlib.pyplot as plt
import numpy as np
x= np.linspace(-np.pi,np.pi,100) # x轴的定义域为 -3.14~3.14,中间间隔100个元素
plt.plot(x,np.sin(x))
#显示所画的图
plt.show()
image.png
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-np.pi * 2, np.pi * 2, 100) # 定义域为: -2pi 到 2pi
plt.figure(1, dpi=50) # 创建图表1,dpi是精度
for i in range(1, 5): # 画四条线
plt.plot(x, np.sin(x / i))
plt.show()
image.png
plt.figure(1, dpi=50) # 创建图表1,dpi代表图片精细度,dpi越大文件越大,杂志要300以上
data = [1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 4]
plt.hist(data) # 只要传入数据,直方图就会统计数据出现的次数
plt.show()
image.png
x = np.arange(1,10)
y = x
fig = plt.figure()
plt.scatter(x,y,c = 'r',marker = 'o') #c = 'r'表示散点的颜色为红色,marker 表示指定三点多形状为圆形
plt.show()
image.png
pandas和matplotlib相结合使用
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
iris = pd.read_csv("./iris_training.csv")#用pandas导入iris数据集
print (iris.head())#显示前五行
# 输出结果如下
120 4 setosa versicolor virginica
0 6.4 2.8 5.6 2.2 2
1 5.0 2.3 3.3 1.0 1
2 4.9 2.5 4.5 1.7 2
3 4.9 3.1 1.5 0.1 0
4 5.7 3.8 1.7 0.3 0
#绘制散点图
iris.plot(kind="scatter", x="120", y="4")#使用iris数据集120和4列画散点图
plt.show()# 只是让pandas 的plot() 方法在pyCharm上显示
image.png
seaborn库安装
⮀ ~/Python ⮀ pip3 install seaborn
Collecting seaborn
Downloading https://files.pythonhosted.org/packages/a8/76/220ba4420459d9c4c9c9587c6ce607bf56c25b3d3d2de62056efe482dadc/seaborn-0.9.0-py3-none-any.whl (208kB)
100% |████████████████████████████████| 215kB 92kB/s
Requirement already satisfied: numpy>=1.9.3 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from seaborn) (1.15.4)
Collecting scipy>=0.14.0 (from seaborn)
Downloading https://files.pythonhosted.org/packages/c0/1d/eef9d7b34ab8b7ee42d570f2e24d58ee0374064c1ca593bdb02914f66a80/scipy-1.2.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (28.8MB)
100% |████████████████████████████████| 28.8MB 25kB/s
Requirement already satisfied: pandas>=0.15.2 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from seaborn) (0.23.4)
Requirement already satisfied: matplotlib>=1.4.3 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from seaborn) (3.0.2)
Requirement already satisfied: pytz>=2011k in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from pandas>=0.15.2->seaborn) (2018.9)
Requirement already satisfied: python-dateutil>=2.5.0 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from pandas>=0.15.2->seaborn) (2.7.5)
Requirement already satisfied: cycler>=0.10 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from matplotlib>=1.4.3->seaborn) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from matplotlib>=1.4.3->seaborn) (1.0.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from matplotlib>=1.4.3->seaborn) (2.3.1)
Requirement already satisfied: six>=1.5 in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from python-dateutil>=2.5.0->pandas>=0.15.2->seaborn) (1.11.0)
Requirement already satisfied: setuptools in /Users/.virtualenvs/py3env/lib/python3.6/site-packages (from kiwisolver>=1.0.1->matplotlib>=1.4.3->seaborn) (40.4.1)
Installing collected packages: scipy, seaborn
Successfully installed scipy-1.2.0 seaborn-0.9.0
seaborn画图
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
#去除告警信息
import warnings
warnings.filterwarnings("ignore")
iris = pd.read_csv("./iris_training.csv")
#设置样式
sns.set(style="white", color_codes=True)
# 设置绘制格式为散点图
sns.jointplot(x="120", y="4", data=iris, size=5)
# distplot绘制曲线
sns.distplot(iris['120'])
# 只是让pandas 的plot() 方法在pyCharm上显示
plt.show()
image.png
增加颜色分类显示
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
iris = pd.read_csv("./iris_training.csv")
sns.set(style="white", color_codes=True)
# FacetGrid 一般绘图函数
# hue 彩色显示分类0/1/2
# plt.scatter 绘制散点图
# add_legend() 显示分类的描述信息
sns.FacetGrid(iris, hue="virginica", size=5).map(plt.scatter, "120", "4").add_legend()#通过map选取120和4列数据
sns.FacetGrid(iris, hue="virginica", size=5).map(plt.scatter, "setosa", "versicolor").add_legend()#通过map选取setosa和versicolor列数据
# 只是让pandas 的plot() 方法在pyCharm上显示
plt.show()
image.png
image.png