赞
踩
可视化是在整个数据挖掘的关键辅助工具,可以清晰的理解数据,从而调整我们的分析方法。
例如下面两个图为数字展示和图形展示:
import matplotlib.pyplot as plt
if __name__ == "__main__":
plt.figure(figsize=(20,8)) # 画布大小,可以不写或参数为空,则使用默认大小
plt.plot([1,2,3], [4,5,6]) # x轴坐标, y轴
plt.show()
容器层主要由Canvas、Figure、Axes组成。
Canvas是位于最底层的系统层,在绘图的过程中充当画板的角色,即放置画布(Figure)的工具。
Figure是Canvas上方的第一层,也是需要用户来操作的应用层的第一层,在绘图的过程中充当画布的角色。
Axes是应用层的第二层,在绘图的过程中相当于画布上的绘图区的角色。
特点为:
辅助显示层为Axes(绘图区)内的除了根据数据绘制出的图像以外的内容,主要包括
该层的设置可使图像显示更加直观更加容易被用户理解,但又不会对图像产生实质的影响
图像层指Axes内通过plot、scatter、bar、histogram、pie等函数根据数据绘制出的图像。
为了更好地理解所有基础绘图功能,我们通过天气温度变化的绘图来融合所有的基础API使用
matplotlib.pyplot模块
matplotlib.pytplot包含了一系列类似于matlab的画图函数。 它的函数作用于当前图形(figure)的当前坐标系(axes)。
import matplotlib.pyplot as plt
展现上海一周的天气,比如从星期一到星期日的天气温度如下
# 1)创建画布(容器层)
plt.figure()
# 2)绘制折线图(图像层)
plt.plot([1, 2, 3, 4, 5, 6 ,7], [17, 17, 18, 15, 11, 11, 13])
# 3)显示图像
plt.show()
plt.figure(figsize=(), dpi=)
figsize:指定图的长宽
dpi:图像的清晰度
返回fig对象
plt.savefig(path)
# 1)创建画布,并设置画布属性
plt.figure(figsize=(20, 8), dpi=80)
# 2)保存图片到指定路径
plt.savefig("test.png")
注意:plt.show()会释放figure资源,如果在显示图像之后保存图片将只能保存空图片。
需求:画出某城市11点到12点1小时内每分钟的温度变化折线图,温度范围在15度~18度
效果:
import random
import matplotlib.pyplot as plt
# 生成温度数据
x = range(60)
shanghai = [random.uniform(15,18) for _ in x]
plt.figure(figsize=(10,4), dpi=80)
# 绘制图形
plt.plot(x,shanghai)
plt.xticks(x_value, [f"11:{i}" for i in x_value])
# 显示图像
plt.show()
plt.xticks(x, **kwargs)
x:要显示的刻度值
plt.yticks(y, **kwargs)
y:要显示的刻度值
# 生成温度数据
x = range(60)
shanghai = [random.uniform(15,18) for _ in x]
plt.figure(figsize=(10,4), dpi=80)
# 绘制图形
plt.plot(x,shanghai)
# 设置辅助显示层
plt.yticks(range(0, 41)[::5])
x_value = x[::5]
plt.xticks(x_value, [f"11:{i}" for i in x_value])
# 显示图像
plt.show()
下载中文字体(黑体,看准系统版本)
下载 SimHei 字体(或者其他的支持中文显示的字体也行)
1) 安装字体
sudo cp ~/SimHei.ttf /usr/share/fonts/SimHei.ttf
注)Linux如果用ubantu也可以通过双击安装
2) 删除matplotlib缓存文件
Mac系统的解决方案:
cd ~/.matplotlib
rm -r *
Linux系统的解决方案
cd ~/.cache/matplotlib
rm -r *
vi ~/.matplotlib/matplotlibrc
将文件内容修改为:
font.family : sans-serif
font.sans-serif : SimHei
axes.unicode_minus : False
Linux系统的解决方案
修改配置文件
sudo find -name matplotlibrc
返回结果:
./.virtualenvs/ai/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
打开配置文件:
vi ./.virtualenvs/ai/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
将配置文件中下面3项改为如下所示:
font.family : sans-serif
font.sans-serif : SimHei
axes.unicode_minus : False
为了更加清楚地观察图形对应的值
颜色字符 | 风格字符 |
---|---|
r 红色 | - 实线 |
g 绿色 | - - 虚线 |
b 蓝色 | -. 点划线 |
w 白色 | : 点虚线 |
c 青色 | ’ ’ 留空、空格 |
m 洋红 | |
y 黄色 | |
k 黑色 |
设置显示图例:
plt.legend(loc="best")
location string | location Code |
---|---|
“best” | 0 |
“upper right” | 1 |
“upper left” | 2 |
“lower left” | 3 |
“lower right” | 4 |
“right” | 5 |
“center left” | 6 |
“center right” | 7 |
“lower center” | 8 |
“upper center” | 9 |
“center” | 10 |
# 生成温度数据 x = range(60) shanghai = [random.uniform(15,18) for _ in x] beijing = [random.uniform(5,8) for _ in x] plt.figure(figsize=(10,4), dpi=80) # 绘制图形 plt.plot(x,shanghai,label="上海") plt.plot(x,beijing, label="北京") # 设置x,y轴的显示 plt.yticks(range(0, 41)[::5]) x_value = x[::5] plt.xticks(x_value, [f"11点{i}" for i in x_value]) # 设置图像标题和描述信息 plt.title("温度变化", fontsize=20)# 添加标题并设置字体大小 plt.xlabel("时间", color='r',fontsize=16) plt.ylabel("温度", color='r',fontsize=16) # 绘制网格 plt.grid(True, linestyle='--', alpha=0.5) # 设置显示图例说明 plt.legend(loc=1) # 显示图像 plt.show()
如果我们想要将上海和北京的天气图显示在同一个图的不同坐标系当中,效果如下
可以通过subplots函数实现(旧的版本中有subplot,使用起来不方便),推荐subplots函数
Parameters:
nrows, ncols : int, optional, default: 1, Number of rows/columns of the subplot grid.
**fig_kw : All additional keyword arguments are passed to the figure() call.
Returns:
fig : 图对象
ax :
设置标题等方法不同:
set_xticks
set_yticks
set_xlabel
set_ylabel
关于axes子坐标系的更多方法:参考https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes
# 生成温度数据 x = range(60) shanghai = [random.uniform(15,18) for _ in x] beijing = [random.uniform(5,8) for _ in x] # 1.创建画布 fig,axes = plt.subplots(nrows=1,ncols=2,figsize=(20,8),dpi=80) # 2.绘制图形 shanghai_plot = axes[0] beijing_plot = axes[1] shanghai_plot.plot(x,shanghai,label="上海") beijing_plot.plot(x,beijing, label="北京") # 3.设置辅助显示 shanghai_plot.set_title("上海温度变化", fontsize=20)# 添加标题并设置字体大小 beijing_plot.set_title("北京温度变化", fontsize=20) x_value = x[::5] for axe in axes: axe.set_xlabel("时间", color='r',fontsize=16)# 添加描述信息 axe.set_ylabel("温度", color='r',fontsize=16)# 添加描述信息 # 设置说明 axe.set_yticks(range(0, 41)[::5]) axe.set_xticks(x_value, [f"11点{i}" for i in x_value]) # 设置网格 axe.grid(True, linestyle='--', alpha=0.5) # 设置显示图例说明 axe.legend(loc=1)
...
import numpy as np
# 得到等间隔的数据
data = np.linspace(-10,10,10000)
# 绘制图形
plt.figure(figsize=(20,8), dpi=100)
x = data
y = np.sin(x)
plt.plot(x,y)
plt.show()
Matplotlib能够绘制折线图、散点图、柱状图、直方图、饼图。
需求:探究房屋面积和房屋价格的关系
房屋面积数据:
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64,
163.56, 120.06, 207.83, 342.75, 147.9 , 53.06, 224.72, 29.51,
21.61, 483.21, 245.25, 399.25, 343.35]
房屋价格:
y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9 , 239.34,
140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1 ,
30.74, 400.02, 205.35, 330.64, 283.45]
import matplotlib.pyplot as plt # 准备数据 # 房屋面积数据 x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64, 163.56, 120.06, 207.83, 342.75, 147.9 , 53.06, 224.72, 29.51, 21.61, 483.21, 245.25, 399.25, 343.35] # 房屋价格: y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9 , 239.34, 140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1 , 30.74, 400.02, 205.35, 330.64, 283.45] # 创建画布 plt.figure(figsize=(20,8), dpi=100) # 创建散点图 plt.scatter(x,y) # 添加辅助层 # 设置间距 plt.xticks(range(int(max(x))+1)[::10]) plt.yticks(range(int(max(y))+1)[::20]) # 图表标题与 plt.title("房价与面积的关系", fontsize=22) plt.xlabel("面积(m²)", fontsize=18,color="r") plt.ylabel("价格(元)",fontsize=18, color='r') # 显示网格 plt.grid(True, linestyle="--", alpha=0.5) # 显示图像 plt.show()
应用场景
matplotlib.pyplot.bar(x, width, align='center', **kwargs) Parameters: x : sequence of scalars. width : scalar or array-like, optional 柱状图的宽度 align : {‘center’, ‘edge’}, optional, default: ‘center’ Alignment of the bars to the x coordinates: ‘center’: Center the base on the x positions. ‘edge’: Align the left edges of the bars with the x positions. 每个柱状图的位置对齐方式 **kwargs : color:选择柱状图的颜色 Returns: `.BarContainer` Container with all the bars and optionally errorbars.
['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴', '降魔传','追捕','七十七天','密战','狂兽','其它']
[73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
代码实现:
import matplotlib.pyplot as plt # 导入数据 title = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴', '降魔传','追捕','七十七天','密战','狂兽','其它'] box_income = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222] # 绘制图像 plt.figure(figsize=(20,8), dpi=80) plt.bar(title, box_income, width=0.5, color=['b','r','g','y','c','m','y','k','c','g','b']) # 辅助层 plt.yticks(range(max(box_income))[::5000]) plt.xlabel("电影名称", fontsize=20, color='r') plt.ylabel("票房收入(万元)", fontsize=20, color='r') plt.title("x月电影票房统计", fontsize=26, color='c') plt.grid(True, linestyle='--', alpha=0.5) # 显示图像 plt.show()
比较相同天数的票房
有时候为了公平起见,需要对比不同电影首日和首周的票房
1 准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记']
first_day = [10587.6,10062.5,1275.7]
first_weekend=[36224.9,34479.6,11830]
数据来源: https://piaofang.maoyan.com/?ver=normal
2 绘制
... movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记'] first_day = [10587.6,10062.5,1275.7] first_weekend=[36224.9,34479.6,11830] # 绘制图像 x = range(len(movie_name)) plt.bar(x, first_day, width=0.2, label="首日票房" ) plt.bar([i+0.2 for i in x],first_weekend, width=0.2,label="首周票房") # 说明 plt.xticks(x,movie_name) # 显示图例 plt.legend() # 显示图像 plt.show()
柱状图应用场景
适合用在分类数据对比场景上
直方图,形状类似柱状图却有着与柱状图完全不同的含义。直方图牵涉统计学的概念,首先要对数据进行分组,然后统计每个分组内数据元的数量。 在坐标系中,横轴标出每个组的端点,纵轴表示频数,每个矩形的高代表对应的频数,称这样的统计图为频数分布直方图。
相关概念:
需求:电影时长分布状况
现有250部电影的时长,希望统计出这些电影时长的分布状态(比如时长为100分钟到120分钟电影的数量,出现的频率)等信息,你应该如何呈现这些数据?
数据:
time = [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]
matplotlib.pyplot.hist(x, bins=None, normed=None, **kwargs)
Parameters:
x : (n,) array or sequence of (n,) arrays
bins : integer or sequence or ‘auto’, optional
组数
代码实现:
import matplotlib.pyplot as plt # 准备数据 time_ = [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] # 画布 plt.figure(figsize=(20,8),dpi=100) # 设置组距 distance = 2 # 计算组数 group_num = int((max(time_)-min(time_))//distance) # 绘制 plt.hist(time_, bins=group_num) # 显示刻度 plt.xticks(range(min(time_), max(time_))[::2]) # 添加说明xinxi plt.ylabel("电影数量", fontsize=20) plt.xlabel("电影时长(小时)", fontsize=20) plt.title("250部电影时长分布",fontsize=24) plt.grid(True, linestyle="--", alpha=0.5) # 显示图像 plt.show()
例如:用户年龄分布,商品价格分布
饼图广泛得应用在各个领域,用于表示不同分类的占比情况,通过弧度大小来对比各种分类。饼图通过将一个圆饼按照分类的占比划分成多个区块,整个圆饼代表数据的总量,每个区块(圆弧)表示该分类占总体的比例大小,所有区块(圆弧)的加和等于 100%。
需求:显示不同的电影的排片占比
电影排片:
效果:
数据:
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]
注意显示的百分比的位数
plt.pie(x, labels=,autopct=,colors)
x:数量,自动算百分比
labels:每部分名称
autopct:占比显示指定%1.2f%%
colors:每部分颜色
代码实现:
import matplotlib.pyplot asplt # 准备数据 movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它'] place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105] # 绘制图像 plt.figure(figsize=(20, 8), dpi=100) plt.pie(place_count, labels=movie_name,autopct="%1.2f%%",colors=['b','r','g','y','c','m','y','c','g','y']) # 显示图例 plt.legend() # 添加标题 plt.title("电影排片占比") # 规定为正圆 plt.axis('equal') # 显示图像 plt.show()
添加explode属性
# 准备数据 movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它'] place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105] explode = (0.1, 0, 0.1, 0, 0, 0, 0, 0, 0, 0, 0) # 绘制图像 plt.figure(figsize=(20, 8), dpi=100) plt.pie(place_count, labels=movie_name,explode=explode,autopct="%1.2f%%",colors=['b','r','g','y','c','m','y','c','g','y']) # 显示图例 plt.legend() # 添加标题 plt.title("电影排片占比") # 规定为正圆 plt.axis('equal') # 显示图像 plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。