数据清洗 Chapter02 | Matplotlib常用图形

这篇文章讲述的是Matplotlib常用图形。希望这篇数据清洗的文章对您有所帮助!如果您有想学习的知识或建议,可以给作者留言~


一、可视化探索

数据的初步诊断

  • 数据清洗的初始阶段,需要初步认识数据集的特点以及构成

若数据量适中,人为观察是直观可行的方式
一般情况下,需要借助外部工具和手段进行数据的初步判定

  • 数据可视化

数据可视化借助图形,图标等手段,从数据集中抽取有效信息,并对其进行展观
从视觉上快速感知、了解数据集
多维度观察“平面”数据

二、Matplotlib

Matplotlib是Python最著名的绘图库
提供于Matlab相似的命令API,适合交互式制图
方便地将它作为绘图控件,嵌入到GUI应用程序中
绘图文档完备,在Gallery页面中有上百幅缩略图和源程序可供查看

1、Matplotlib简介

采集数据
数据可视化交接数据统计特征
可绘制散点图,折线图,分布图,箱线图等
充分了解数据,再进行数据分析

  • 1、创建空图
import pandas as pd
import numpy as np
iris = pd.read_csv('./iris.csv')
import matplotlib.pyplot as plt 
%matplotlib inline
  • 2、创建子图
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)

2

  • 3、查看iris的内容
    3

2、折线图

  • 1
ax1 = plt.subplot(111)
ax1.plot(iris['Sepal.Length'], 'k--', label='Sepal.Length')    
# 'k--'  为折现的形状或颜色,label为图例的名称
ax1.legend(loc='best')       # 设置图例的位置
ax1

4

  • 2
ax1 = plt.subplot(111)
ax1.plot(iris['Sepal.Length'], 'b-', label='Sepal.Length')
ax1.legend(loc='best')
ax1

5

  • 3
ax1 = plt.subplot(111)
ax1.plot(iris['Sepal.Length'], 'r*', label='Sepal.Length')
ax1.legend(loc='best')
ax1

6

3、散点图

散点图可体现两个属性值的对比

ax2 = plt.subplot(111)
ax2.scatter(iris['Sepal.Width'],iris['Petal.Width'] , label= 'petal&sepal_width')
ax2.legend(loc='best')
plt.xlabel('Sepal.Width')
plt.ylabel('Petal.Width')

7

上图我们可以看到是不相关的,如果相关的话是会聚集到对角线附近

4、条形图

如果想要查看属性的分布情况时,可以使用条形图

ax3 = plt.subplot(111)
a = ax3.hist(iris['Sepal.Width'],bins =10, label='Sepal.Width')
ax3.legend(loc='best')

8

5、drawstyle

drawstyle用于选择插值方式

plt.plot(iris['Petal.Length'], drawstyle = 'steps')

9

6、plt.xlim()&plt.ylim()

用于设置坐标轴范围

  • 1、plt.xlim()
plt.plot(iris['Petal.Length'], drawstyle = 'steps')
plt.xlim([0, 50])

10

  • 2、plt.ylim()
plt.plot(iris['Petal.Length'], drawstyle = 'steps')
plt.ylim([0, 50])

11

  • 3、设置坐标位置
ax= plt.subplot(111)

# 设置坐标轴现实 
ax.plot(iris['Sepal.Length'], 'g-', label = '_nolengend_')

# 设置坐标位置
ticks = ax.set_xticks([30, 60 , 90, 120])

# 设置该位置显示的文字,以及文字旋转角度,文字大小
labels = ax.set_xticklabels(['30', '60', '90', '120'], rotation = 50, fontsize = 'large')

12

7、箱纸图

箱纸图可用于显示数据的分布情况

ax= plt.subplot(111)

# whis指定离群点分割线
ax.boxplot(iris['Sepal.Length'], whis=2)

# plt.title()设置标题
plt.title('box plot')

# plt.ylabel()设置坐标轴名称
plt.ylabel('data content')

13


各位路过的朋友,如果觉得可以学到些什么的话,点个赞再走吧,欢迎各位路过的大佬评论,指正错误,也欢迎有问题的小伙伴评论留言,私信。每个小伙伴的关注都是本人更新博客的动力!!!

发布了57 篇原创文章 · 获赞 65 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_16146103/article/details/105431062