绘制简单图, 如直方图, 计数图, 散点图等.
python 测试开发项目实战 - 目录 https://china-testing.github.io/practices.html
python 工具书籍下载 - 持续更新 https://china-testing.github.io/python_books.html
python 3.7 极速入门教程 - 目录 https://china-testing.github.io/python3_quick.html
数据集: Titanic 号
数据集: Titanic 号训练集的乘客信息 下载地址: https://itbooks.pipipan.com/fs/18113597-326184444
导入数据
- import numpy as np
- import matplotlib.pyplot as plt
- import pandas as pd
- titanic_data = pd.read_csv(r"D:\Datasets\train.csv")
- titanic_data.head()
image.PNG
该数据集包含 1912 年 Titanic 号的乘客信息, 包括姓名, 年龄, 乘客等级, 是否幸存等.
参考资料
讨论 qq 群 630011153 144081101 567351477
本文最新版本地址
本文涉及的 python 测试开发库 https://github.com/china-testing/python-api-tesing 谢谢点赞!
本文相关海量书籍下载
python 工具书籍下载 - 持续更新 https://china-testing.github.io/python_books.html
python GUI 工具书籍下载 - 持续更新
直方图
titanic_data['Age'].hist()
使用 Pandas 数据框绘制年龄列的直方图是多么容易.
image.PNG
可以将 Matplotlib 的参数传递给 hist() 方法, 因为 Pandas 在使用了 Matplotlib 库.
titanic_data['Age'].hist(bins=20)
image.PNG
通过导入 Seaborn 库设置 set_style 属性值来改进图的样式. 例如, 让我们将网格的样式设置为深灰色.
- import seaborn as sns
- sns.set_style('darkgrid')
- titanic_data['Age'].plot(kind='hist', bins=20)
数据帧有两种方法绘制图形. 一种方法是给传递 plot 函数传递 kind 参数:
titanic_data['Age'].plot(kind='hist', bins=20)
另一种方法是使用 plot 函数直接调用绘图的方法, 参见前面的例子.
折线图
要使用 Pandas 数据帧绘制折线图, 您必须使用 plot 函数调用 line() 方法并传递 x 和 y 轴的值, 如下所示:
titanic_data.plot.line(x='Age', y='Fare', figsize=(8,6))
x 轴包含乘客的年龄, 而 y 轴包含乘客支付的票价. figsize 属性来改变绘图的大小, 特别注意这个单位是英尺.
image.PNG
散点图
titanic_data.plot.scatter(x='Age', y='Fare', figsize=(8,6))
image.PNG
箱体图
titanic_data.plot.box(figsize=(10,8))
image.PNG
六角形图
六边形图绘制了在 x 和 y 轴上交叉数据点的六边形. 点越多, 六边形越暗.
titanic_data.plot.hexbin(x='Age', y='Fare', gridsize=30, figsize=(8,6))
image.PNG
密度图
titanic_data.plot.hexbin(x='Age', y='Fare', gridsize=30, figsize=(8,6))
image.PNG
来源: http://www.jianshu.com/p/d90a6643ea20