Matplotlib介绍

  • 绘图是数据分析工作中最重要的任务之一,将各种数据以图形的方式表现出来更加直观。
  • Matplotlib模块是Python数据分析、数据探索中数据可视化的重要模块。
  • 可以生成点线图plot柱状图bar饼状图pie散点图scatter直方图hist等等各种数据图表

快速绘制点线图

import matplotlib.pyplot as plt
# 数量 评分 类型
info0 = [(1, 6, '剧情'), (4, 8, '剧情'), (12, 9, '剧情'), (3, 10, '剧情')]
info1 = [(4, 7, '喜剧'), (2, 8, '喜剧'), (14, 9, '喜剧')]
info2 = [(3, 6, '动作'), (4, 7, '动作'), (8, 8, '动作'), (5, 9, '动作')]
info3 = [(1, 3, '爱情'), (1, 7, '爱情'), (6, 8, '爱情'), (9, 9, '爱情'), (3, 10, '爱情')]

# 得到画布
ax = plt.subplot()
# 在同一画布上绘制4条曲线
plt.plot([x[1] for x in info0],[y[0] for y in info0] ,'bp-',label = "plot")
plt.plot([x[1] for x in info1],[y[0] for y in info1] ,'mp-',label = "comedy")
plt.plot([x[1] for x in info2],[y[0] for y in info2] ,'yp-',label = "action")
plt.plot([x[1] for x in info3],[y[0] for y in info3] ,'gp-',label = "love")
#分别设置x,y轴标题
ax.set_xlabel('Rate')
ax.set_ylabel('Numbers')
# 设置x轴刻度标签列表
group_labels=['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct',
              'Nov','Dec']
# 设置x轴刻度标签、角度rotation
plt.xticks([3,4,5,6,7,8,9,10],[3,4,5,6,7,8,9,10], rotation=-45)
# 设置标题
plt.title('Film Classification ')
# 绘制网格
plt.grid()
# 显示画线时的label标签  自动在最合适的位置显示
plt.legend(loc='best')
plt.show()

python 动态绘制点云数据 python绘制点状图_饼图

快速绘制柱状图

import matplotlib.pyplot as plt
# 数量 评分 类型
info0 = [(1, 6, '剧情'), (4, 8, '剧情'), (12, 9, '剧情'), (3, 10, '剧情')]
info1 = [(4, 7, '喜剧'), (2, 8, '喜剧'), (14, 9, '喜剧')]
info2 = [(3, 6, '动作'), (4, 7, '动作'), (8, 8, '动作'), (5, 9, '动作')]
info3 = [(1, 3, '爱情'), (1, 7, '爱情'), (6, 8, '爱情'), (9, 9, '爱情'), (3, 10, '爱情')]

# 得到画布
ax = plt.subplot()
# 在同一画布上绘制4条曲线
plt.plot([x[1] for x in info0],[y[0] for y in info0] ,'bp-',label = "plot")
plt.plot([x[1] for x in info1],[y[0] for y in info1] ,'mp-',label = "comedy")
plt.plot([x[1] for x in info2],[y[0] for y in info2] ,'yp-',label = "action")
plt.plot([x[1] for x in info3],[y[0] for y in info3] ,'gp-',label = "love")
#分别设置x,y轴标题
ax.set_xlabel('Rate')
ax.set_ylabel('Numbers')
# 设置x轴刻度标签列表
group_labels=['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct',
              'Nov','Dec']
# 设置x轴刻度标签、角度rotation
plt.xticks([3,4,5,6,7,8,9,10],[3,4,5,6,7,8,9,10], rotation=-45)
# 设置标题
plt.title('Film Classification ')
# 绘制网格
plt.grid()
# 显示画线时的label标签  自动在最合适的位置显示
plt.legend(loc='best')
plt.show()

python 动态绘制点云数据 python绘制点状图_数据_02

快速绘制饼状图

import matplotlib.pyplot as plt

# 数据
info =  ((25, 'Biography'), (18, 'Adventure'), (23, 'Plot'), (28, 'Action'), (13, 'Animation'), (9, 'History'), (30, 'Comedy'), (18, 'Fantasy'), (22, 'Terror'))
# 绘制饼图

bars = plt.pie([y[0] for y in info],labels=[x[1] for x in info])
# 得到画布
ax = plt.subplot()
# 设置图标标题
ax.set_title('Number of films')
# 显示为圆形(避免比例压缩为椭圆)
plt.axis('equal')

plt.show()

python 动态绘制点云数据 python绘制点状图_数据_03

快速绘制散点图

import matplotlib.pyplot as plt
#数据
info =  ((25, 'Biography'), (18, 'Adventure'), (23, 'Plot'), (28, 'Action'), (13, 'Animation'), (9, 'History'), (30, 'Comedy'), (18, 'Fantasy'), (22, 'Terror'))
x = [x[0] for x in info]
y = [y[1] for y in info]
# 画点
# marker代表点的形状
plt.scatter(x,y,marker="*")
# 网格
plt.grid(linestyle = '--')
# 显示图
plt.show()

python 动态绘制点云数据 python绘制点状图_柱状图_04

多个子表图显示

我们可以将以上4个图在同一张图上显示,将4部分代码糅合在一块即可。

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
# 创建三张子图标
# 位置是按照从左往右,从上到下自动排列
# 画图时,在没有指定子图表时默认为最后一个
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

info0 = [(1, 6, '剧情'), (4, 8, '剧情'), (12, 9, '剧情'), (3, 10, '剧情')]
info1 = [(4, 7, '喜剧'), (2, 8, '喜剧'), (14, 9, '喜剧')]
info2 = [(3, 6, '动作'), (4, 7, '动作'), (8, 8, '动作'), (5, 9, '动作')]
info3 = [(1, 3, '爱情'), (1, 7, '爱情'), (6, 8, '爱情'), (9, 9, '爱情'), (3, 10, '爱情')]

ax1.plot([x[1] for x in info0],[y[0] for y in info0] ,'bp-',label = "plot")
ax1.plot([x[1] for x in info1],[y[0] for y in info1] ,'mp-',label = "comedy")
ax1.plot([x[1] for x in info2],[y[0] for y in info2] ,'yp-',label = "action")
ax1.plot([x[1] for x in info3],[y[0] for y in info3] ,'gp-',label = "love")
ax1.set_xlabel('Rate')
ax1.set_ylabel('Numbers')
# 设置x轴刻度标签列表
group_labels=['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct',
              'Nov','Dec']
# 设置x轴刻度标签、角度rotation
# ax1.set_xticks([3,4,5,6,7,8,9,10],[3,4,5,6,7,8,9,10], rotation=-45)
# 设置标题
ax1.set_title('Film Classification ')
ax1.grid()
ax1.legend(loc='best')


info =  ((25, 'Biography'), (18, 'Adventure'), (23, 'Plot'), (28, 'Action'), (13, 'Animation'), (9, 'History'), (30, 'Comedy'), (18, 'Fantasy'), (22, 'Terror'))
bars = ax2.bar([x[1] for x in info],[y[0] for y in info])
ax2.grid(linestyle = "--")
colors = ['#084B8A','#E6E0F8','#9AFE2E','#886A08','#0B3B39','#8A0829','#F5A9BC','#5F04B4','#E2A9F3']
for bar, color in zip(bars, colors):
      bar.set_color(color)
# 设置y轴标题
ax2.set_ylabel('number')
# 设置x轴标题
ax2.set_xlabel('type')
# 设置标题
ax2.set_title('Number of films')
# 为每个柱状块添加具体文字数据
for x,y in zip([x[1] for x in info],[y[0] for y in info]):
    ax2.text(x, y+0.05, '%s' % y, ha='center', va= 'bottom')


info =  ((25, 'Biography'), (18, 'Adventure'), (23, 'Plot'), (28, 'Action'), (13, 'Animation'), (9, 'History'), (30, 'Comedy'), (18, 'Fantasy'), (22, 'Terror'))
# 绘制饼图
# labels为每个区域设置文字描述
ax3.pie([y[0] for y in info],labels=[x[1] for x in info])
# 设置图标标题
ax3.set_title('Number of films')
# 显示为圆形(避免比例压缩为椭圆)
ax3.axis('equal')

info =  ((25, 'Biography'), (18, 'Adventure'), (23, 'Plot'), (28, 'Action'), (13, 'Animation'), (9, 'History'), (30, 'Comedy'), (18, 'Fantasy'), (22, 'Terror'))
x = [x[0] for x in info]
y = [y[1] for y in info]
# 画点
# marker代表点的形状
ax4.scatter(x,y,marker="*")
# 虚线
ax4.grid(linestyle = '--')

# 显示
plt.show()

python 动态绘制点云数据 python绘制点状图_python 动态绘制点云数据_05