matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建
官方文档:https://matplotlib.org/stable/gallery/index
官方图表展示(部分):https://matplotlib.org/stable/plot_types/index.html
matplotlib模块的使用
绘制折线图
matplotlib使用展示
from matplotlib import pyplot as plt
x = range(2, 26, 2) # x轴数据
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15] # y轴数据
plt.plot(x, y) # 传入x轴、y轴的数据
plt.show() # 展示绘图结果
设置绘图大小和刻度、保存绘图结果
from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 27, 22, 18, 15]
plt.figure(figsize=(10, 6), dpi=80) # 设置图片大小,单位是英寸,dpi表示每英寸像素的数量
plt.plot(x, y) # 绘图
plt.xticks(range(2, 25)) # 间隔为1设置x轴的刻度
plt.yticks(range(min(y), max(y)+1)) # 设置y轴的刻度
plt.savefig('t1.png') # 保存绘图结果
设置刻度和中文字体
from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager
"""设置字体"""
# 适用于Windows/Linux/Mac
my_font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf')
x = range(120)
y = [random.randint(20, 35) for i in range(120)]
plt.figure(figsize=(14, 6), dpi=80) # 调整图片大小
plt.plot(x, y) # 绘图
"""调整x轴的刻度"""
x_ticks = ['10点{}分'.format(i) for i in range(60)]
x_ticks += ['11点{}分'.format(i) for i in range(60)]
# 取步长,数字和字符串需要一一对应,数据的长度和个数要一样
# rotation设置旋转的度数,fontproperties设置字体
plt.xticks(list(x)[::5], x_ticks[::5], rotation=45, fontproperties=my_font)
plt.show()
添加描述信息
from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
x = range(120)
y = [random.randint(20, 35) for i in range(120)]
plt.figure(figsize=(15, 8), dpi=60)
plt.plot(x, y)
my_font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf')
x_ticks = ['10点{}分'.format(i) for i in range(60)]
x_ticks += ['11点{}分'.format(i) for i in range(60)]
plt.xticks(list(x)[::5], x_ticks[::5], rotation=45, fontproperties=my_font)
"""添加描述信息"""
plt.xlabel('时间', fontproperties=my_font) # 设置x轴信息
plt.ylabel('温度/(℃)', fontproperties=my_font) # 设置y轴信息
plt.title('10点-12点每分钟气温变化情况', fontproperties=my_font) # 设置标题
plt.show()
绘制网格和多条折线、添加图例
from matplotlib import pyplot as plt
from matplotlib import font_manager
x = range(11, 31)
a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
b = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
plt.figure(figsize=(10, 5)) # 设置图片大小
"""绘制多条折线"""
# label设置图例上的说明文字,color设置颜色,linestyle设置线条样式
plt.plot(x, a, label='自己', color='red', linestyle=':')
plt.plot(x, b, label='同桌', color='#F9F900', linestyle='--')
my_font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf')
x_ticks = ['{}岁'.format(i) for i in x]
plt.xticks(x, x_ticks, fontproperties=my_font)
"""绘制网格"""
# alpha设置透明度,color设置颜色,linestyle设置线条样式
plt.grid(alpha=0.2, color='green', linestyle='-.')
"""添加图例"""
# 将折线上的说明添加到图例,prop设置字体,loc设置位置
plt.legend(prop=my_font, loc='upper left')
plt.show()
绘制其他图表
绘制散点图
使用scatter()
方法绘制散点图,这是和绘制折线图唯一的区别
from matplotlib import pyplot as plt
from matplotlib import font_manager
y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]
x_3 = range(1, 32)
x_10 = range(51, 82)
plt.figure(figsize=(15, 8), dpi=70) # 设置图片大小
# 使用scatter()方法绘制散点图
plt.scatter(x_3, y_3, label='3月')
plt.scatter(x_10, y_10, label='10月')
# 调整x轴的刻度
my_font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf')
x = list(x_3) + list(x_10)
x_ticks = ['3月{}日'.format(i) for i in x_3]
x_ticks += ['10月{}日'.format(i-50) for i in x_10]
plt.xticks(x[::3], x_ticks[::3], rotation=45, fontproperties=my_font)
# 添加图例
plt.legend(prop=my_font, loc='upper left')
# 添加描述信息
plt.xlabel('时间', fontproperties=my_font)
plt.ylabel('温度', fontproperties=my_font)
plt.title('这是个标题', fontproperties=my_font)
plt.show()
绘制条形图
-
绘制纵向条形图
使用bar()
方法绘制纵向条形图from matplotlib import pyplot as plt from matplotlib import font_manager a = ["战狼2", "速度与激情8", "功夫瑜伽"] b = [26.94, 17.53, 16.49] plt.figure(figsize=(15, 8), dpi=70) # 设置图片大小 # 使用bar()方法绘制纵向条形图 plt.bar(range(len(a)), b, color='green', width=0.3) font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf') plt.xticks(range(len(a)), a, fontproperties=font, rotation=270) plt.show()
-
绘制横向条形图
使用barh()
方法绘制横向条形图from matplotlib import pyplot as plt from matplotlib import font_manager a = ["战狼2", "速度与激情8", "功夫瑜伽"] b = [26.94, 17.53, 16.49] plt.figure(figsize=(15, 8), dpi=70) # 使用barh()方法绘制横向条形图 plt.barh(range(len(a)), b, color='orange', height=0.5) font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf') plt.yticks(range(len(a)), a, fontproperties=font) plt.show()
-
绘制多个条形图
通过设置位置偏移实现在一张表中绘制多个条形图from matplotlib import pyplot as plt from matplotlib import font_manager a = ["猩球崛起3:终极之战", "敦刻尔克", "蜘蛛侠:英雄归来", "战狼2"] b_16 = [15746, 312, 4497, 319] b_15 = [12357, 156, 2045, 168] b_14 = [2358, 399, 2358, 362] plt.figure(figsize=(20, 8), dpi=80) # 设置图片大小 x = list(range(len(a))) bar_width = 0.2 # 绘制条形图,并设置位置偏移 plt.bar([i for i in x], b_14, width=bar_width, label='9月14日') plt.bar([i+bar_width for i in x], b_15, width=bar_width, label='9月15日') plt.bar([i+2*bar_width for i in x], b_16, width=bar_width, label='9月16日') # 设置x轴的刻度 font = font_manager.FontProperties(fname=r'C:\Windows\Fonts\simfang.ttf') plt.xticks([i+bar_width for i in x], a, fontproperties=font) # 添加图例 plt.legend(prop=font) plt.show()
绘制直方图
直方图:把数据分组进行统计
组数:将数据分组,当数据在100个以内时,通常分为5-12个组
组距:指每个小组的两个端点的距离。组距最好为极差的因数
组数=极差/组距=(max(a)-min(a))//bin_width
使用hist()
方法绘制直方图,传入需要统计的数据和分组的数量
from matplotlib import pyplot as plt
a = [131, 98, 125, 131, 124, 139, 131]
plt.figure(figsize=(15, 6), dpi=70) # 设置图片大小
bin_width = 3 # 设置组距
num_bins = (max(a)-min(a))//bin_width # 计算组数
"""使用hist()方法绘制直方图,传入需要统计的数据和分组的数量"""
# 默认生成频数分布直方图
plt.hist(a, num_bins)
# 增加density=True,生成频率分布直方图
# plt.hist(a, num_bins, density=True)
# 设置x轴的刻度
plt.xticks(range(min(a), max(a)+bin_width, bin_width))
# 绘制网格
plt.grid()
plt.show()
其他绘图工具
- plotly:可视化工具中的github,相比于matplotlib更加简单,图形更加漂亮,同时兼容matplotlib和pandas,https://plot.ly/python/
- 百度echart:可以进行前端交互的绘图网站,https://echarts.apache.org/zh/index.html
- seaborn:http://seaborn.pydata.org/