【数据可视化】第二章——基于matplotlib的数据可视化

课程代码链接:https://pan.baidu.com/s/1OyzTC-lCwhAOtObyHfA–Q?pwd=abcd
提取码:abcd


系列课程学习目标

  • [✔] 了解Python中的可视化库
  • [✔] 了解NumPy库的基本原理
  • [✔] 掌握matplotlib库的绘图方法
  • 掌握pandas库的绘图方法
  • 掌握seaborn库的绘图方法
  • 掌握Bokeh库的绘图方法
  • 掌握pyqtgraph库的绘图方法

1. 基于matplotlib的数据可视化

Matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建
在这里插入图片描述
Matplotlib库的效果 http://matplotlib.org/gallery.html
在这里插入图片描述
为什么要学习matplotlib?
1.能将数据进行可视化,更直观的呈现
2.使数据更加客观、更具说服力
在这里插入图片描述

2 matplotlib.pyplot函数库简介

1️⃣ Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发。

2️⃣matplotlib.pyplot是绘制各类可视化图形的命令子库,相当于快捷方式。

3️⃣pyplot中的每一个函数都会对画布图像作出相应的改变,如创建画布、在画布中创建一个绘图区、在绘图区上画几条线、给图像添加文字说明等。matplotlib.pyplot中常见函数包含有plt.figure、plt.subplot以及plt.axes。

🔹Matplotlib库的使用

import matplotlib.pyplot as plt
plt.plot([2,1,3,5,4])
plt.show()

在这里插入图片描述
但是目前存在以下几个问题:

1.设置图片大小(想要一个高清大图)
2.保存到本地
3.描述信息,比如x轴和y轴表示什么,这个图表示什么
4.调整x或者y的刻度的间距
5.线条的样式(比如颜色,形状等)
6.标记出特殊的点(比如告诉别人最高点和最低点在哪里)

  1. plt.figure()
    使用plt.figure()函数创建一个全局绘图区域,其中可包含如下参数:

    figsize:设置图像的宽度和高度,单位为英寸
    facecolor:设置图像背景颜色
    dpi:设置绘图对象的分辨率
    edgecolor:设置图像边框颜色

    在这里插入图片描述
    在这里插入图片描述

  2. plt.xticks() /plt.yticks() 设置当前X轴/Y轴刻度位置的值
    在这里插入图片描述
    在这里插入图片描述

  3. plt.subplot(nrows, ncols, plot_number)
    subplot用于在全局绘图区域中创建自绘图区域,其中可包含如下参数:
    nrows:subplot的行数
    ncols:subplot的列数
    plot_number:子图的位置

    ◾使用subplot可以规划figure划分为n个子图,但每条subplot命令只会创建一个子图 。
    ◾在全局绘图区域中创建一个分区体系,并定位到一个子绘图区域。在这里插入图片描述
    🔹plt.subplot(nrows, ncols, plot_number)
    在这里插入图片描述
    在这里插入图片描述

  4. plt.axes()
    ◾plt.axes(rect,facecolor=’w’)创建一个坐标系风格的子绘图区域。
    ◾ 默认创建一个subplot(111) 坐标系,参数rect=[left,bottom,width,height]中4个变量的范围都是[0,1],表示坐标系与全局绘图区域的关系。代码如下:

import matplotlib.pyplot as plt
plt.axes([0.1,0.1,0.7,0.3], facecolor='y')
plt.show()

🔹plt.axes()
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. plt.subplots_adjust()
    ◾plt.subplots_adjust()用于调整子绘图区域的布局。常见语法如下:
    plt.subplots_adjust(left=,bottom=,right=,top=, hspace=)
    left: 画布中子图左边离y轴距离
    bottom: 画布中子图下边离x轴距离
    right: 画布中子图右边离y轴距离
    top: 画布中子图上边离x轴距离
    hspace:子图之间的距离

3 matplotlib.pyplot相关函数简介

在matplotlib.pyplot 库中有plt子库,该子库提供了7个用于读取和显示的函数, 17个用于绘制基础图表的函数,3个区域填充函数,9个坐标轴设置函数以及11个标签与文本设置函数

3.1 plt.plot(x, y, format_string, **kwargs)

plt.plot(x, y, format_string, **kwargs)

x: X轴数据,列表或数组,可选。
y: Y轴数据,列表或数组。
format_string: 控制曲线的格式字符串,可选。
**kwargs: 第二组或更多(x, y, format_string)

当绘制多条曲线时,各条曲线的x不能省略。
在这里插入图片描述
format_string: 控制曲线的格式字符串,可选。
由颜色字符、风格字符和标记字符组成
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

**kwargs: 第二组或更多(x, y, format_string)

color: 控制颜色,color = ‘green’
linestyle: 线条风格,linestyle =--’或 ‘dashed’
linewidth: 线条宽度,linewidth = 3
marker: 标记风格,marker = ‘o’
markerfacecolor: 标记颜色,markerfacecolor = ‘blue’
markersize: 标记尺寸,markersize = 20

在这里插入图片描述
在这里插入图片描述

3.2 plt.title()

fontsize:设置字体大小
fontproperties:设置字体格式
fontweight:设置字体粗细 (‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’)
fontstyle:设置字体类型(‘normal’, ‘italic’ , ‘oblique’)
rotation:旋转角度 vertical, horizontal或数字
backgroundcolor:标题背景颜色

在这里插入图片描述
在这里插入图片描述

import matplotlib.pyplot as plt

fig = plt.figure(figsize = (8,6), dpi = 50)

x = range(0,10,2)
y = [2,1,3,5,4]

plt.plot(x,y,color = 'g', linestyle = '-.',linewidth = 3,
        marker = 'h', markerfacecolor = 'c', markersize = 15)

plt.xticks(x)
plt.yticks(range(min(y),max(y)+1))

plt.title('示例', fontproperties = 'Kaiti', fontsize = 25, backgroundcolor = 'c')

plt.show()

在这里插入图片描述

3.3 plt.xlabel()/ylabel()

fontsize:设置字体大小
fontproperties:设置字体格式
rotation:旋转角度 vertical, horizontal或数字
backgroundcolor:背景颜色
import matplotlib.pyplot as plt

fig = plt.figure(figsize = (8,6), dpi = 50)

x = range(0,10,2)
y = [2,1,3,5,4]

plt.plot(x,y,color = 'g', linestyle = '-.',linewidth = 3,
        marker = 'h', markerfacecolor = 'c', markersize = 15)

plt.xticks(x)
plt.yticks(range(min(y),max(y)+1))

plt.title('示例', fontproperties = 'Kaiti', fontsize = 25)
plt.xlabel('X轴', fontproperties = 'Kaiti', fontsize = 20)
plt.ylabel('Y轴', fontproperties = 'Kaiti', fontsize = 20)
plt.show()

在这里插入图片描述

3.4 plt.legend()

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.5 pyplot的中文显示

3.5.1 第一种方法

pyplot并不默认支持中文显示,需要rcParams修改字体实现。
在这里插入图片描述
在这里插入图片描述

rcParams的属性
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.5.2 第二种方法

在有中文输出的地方,增加一个属性:fontproperties
在这里插入图片描述
在这里插入图片描述

3.6 pyplot的文本显示

文本显示函数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
plt.annotate(s, xy=arrow_crd, xytext=text_crd, arrowprops=dict)
在这里插入图片描述
在这里插入图片描述

3.7 pyplot的子绘图区域

在这里插入图片描述

plt.subplot2grid()
plt.subplot2grid(GridSpec, CurSpec, colspan = 1, rowspan = 1)

理念:设定网格,选中网格,确定选中行列区域数量,编号从0开始。

plt.subplot2grid((3,3),(1,0),colspan=2)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. pyplot的基础图表函数

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

5. numpy和matplotlib绘图综合应用

在这里插入图片描述
在这里插入图片描述

5.1 柱状图

plt.bar()

x:x坐标
height :条形的高度
width :条形的宽度 0~1
bottom :条形的起始位置
color :条形的颜色 'r','b','g','#123465'
edgecolor :边框的颜色
linewidth :边框的宽度*

在这里插入图片描述
在这里插入图片描述
【例1】假设你获取到了2017年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据?

a = ['战狼2','速度与激情8','功夫瑜伽','西游伏妖篇','变形金刚5:最后的骑士','摔跤吧!爸爸','加勒比海盗5:死无对证','金刚:骷髅岛','极限特工:终极回归','生化危机6:终章','乘风破浪','神偷奶爸3','智取威虎山','大闹天竺','金刚狼3:殊死一战','蜘蛛侠:英雄归来','悟空传','银河护卫队2','情圣','新木乃伊']
b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23] 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
【例2】假设你知道了列表a中电影分别在2017-09-14(b_14), 2017-09-15(b_15), 2017-09-16(b_16)三天的票房,为了展示列表中电影本身的票房以及同其他电影的数据对比情况,应该如何更加直观的呈现该数据?

a = ['猩球崛起3:终极之战','敦刻尔克','蜘蛛侠:英雄归来','战狼2']
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]

在这里插入图片描述

5.2 直方图

plt.hist()

x : 指定每个bin分布的数据
Bins: 指定bin的个数
density : 指定密度,也就是每个条状图的占比例比,默认为1
color : 指定条状图的颜色

在这里插入图片描述
在这里插入图片描述

plt.hist()
把数据分为多少组进行统计?
组数要适当,太少会有较大的统计误差,大多规律不明显
组数:将数据分组,当数据在100个以内时,按数据多少常分5-12组
组距:指每个小组的两个端点的距离
组数=极差/组距
在这里插入图片描述
在这里插入图片描述

【例】假设你获取了250部电影的时长(列表a中),希望统计出这些电影时长的分布状态(比如时长为100分钟到120分钟电影的数量,出现的频率)等信息,你应该如何呈现这些数据?

a=[131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.3 散点图

plt.scatter()

x,y:输入数据
s:点的大小
c:点的颜色
marker:点的形状    
alpha:透明度
edgecolors :颜色或颜色顺序

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.4 饼图

plt.pie()

x :每一块的比例
labels :饼图外侧显示的说明文字
explode :距离中心距离
startangle:起始绘制角度
shadow:在饼图下面画一个阴影。默认值:False
labeldistance :label标记的绘制位置,相对于半径的比例
autopct :控制饼图内百分比设置
radius  :控制饼图半径,默认值为1
counterclock :指定指针方向;布尔值

在这里插入图片描述
在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] #设置字体
plt.title('饼图');#设置标题
labels = '计算机系','机械系','管理系','社科系'
sizes = [45,30,15,10] #设置每部分大小
explode = (0,0.0,0,0) #设置每部分凹凸
counterclock = False#设置顺时针方向
plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90) #设置饼图的起始位置
plt.show()

5.5 极坐标图

plt.polar() / projection = ‘polar’

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m 宽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值