matplotlib绘图

1、折线图:

# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午3:42
# @Author  : Felix Wang

from matplotlib import pyplot as plt

"""
    figure图形图标的意思,在这里指的就是我们画的图
    通过实例化一个figure并且传递参数,能够在后台自动使用该figure实例
    在图像模糊的时候可以传入dpi参数,让图片更加清晰
"""
fig = plt.figure(figsize=(20, 8), dpi=80)
x = range(2, 26, 2)
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]
plt.plot(x, y)  # 绘图
# 设置x轴的刻度
_xtick_labels = [i / 2 for i in range(4, 49)]
plt.xticks(_xtick_labels)  # 内部参数为列表
# 设置y轴的刻度
plt.yticks(range(min(y), max(y) + 1))

plt.savefig('sig.svg')  # 保存图片,可以保存png等好几种类型
plt.show()
实例1
# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午4:05
# @Author  : Felix Wang
import random

from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
plt.figure(figsize=(20, 8), dpi=80)  # 设置图像大小
plt.plot(x, y)

_xtick_labels = ['10点{}分'.format(i) for i in range(60)]
_xtick_labels += ['11点{}分'.format(i) for i in range(60)]
plt.xticks(list(x)[::3], _xtick_labels, fontproperties=my_font, rotation=45)  # 两个参数的时候一一对应显示后面的字符串
plt.yticks(range(10, 50))

# 添加描述信息
plt.xlabel('时间',fontproperties=my_font)
plt.ylabel('温度 单位(t)',fontproperties=my_font)
plt.title('10点到12点每分钟的气温变化情况',fontproperties=my_font)

plt.show()
实例2
# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午4:37
# @Author  : Felix Wang

# 官方文档地址:https://matplotlib.org/gallery/index.html

import random
from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

y2 = [random.randint(0, 3) for i in range(20)]
y = [random.randint(0, 3) for i in range(20)]
x = range(11, 31)
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)

# 画图,可以自定义线的分割
plt.plot(x, y, label='自己', color='orange', linestyle=':', linewidth=5)  # 在同一张图画图1,color参数为颜色
plt.plot(x, y2, label='同桌', color='cyan', linestyle='--', alpha=0.8, linewidth=20)  # 在同一张图话图2

# 设置x轴刻度
plt.xticks(x, ['{}岁'.format(i) for i in x], fontproperties=my_font)

# 添加描述信息
plt.xlabel('年龄', fontproperties=my_font)
plt.ylabel('所交女朋友的个数', fontproperties=my_font)
plt.title('11岁到30岁所交女朋友个数统计表', fontproperties=my_font)

# 绘制网格
plt.grid(alpha=0.4)  # alpha表示透明度

# 添加图例
plt.legend(prop=my_font, loc='upper left')  # 添加图例的时候显示中文用prop,loc表示图例的位置

plt.show()
实例3

2、散点图

# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午8:15
# @Author  : Felix Wang

import random
from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

y_3 = [random.randint(8, 15) for i in range(31)]
y_10 = [random.randint(10, 25) for j in range(31)]

x_3 = range(1, 32)
x_10 = range(51, 82)

# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)

# 使用scatter绘制散点图
plt.scatter(x_3, y_3, label="3月份")
plt.scatter(x_10, y_10, label='10月份')

# 调整x轴的刻度
_x = list(x_3) + list(x_10)
_xtick_labels = ['3月{}日'.format(i) for i in x_3]
_xtick_labels += ['10月{}日'.format(i - 50) for i in x_10]

plt.xticks(_x[::3], _xtick_labels[::3], fontproperties=my_font, rotation=45)

# 添加图例
plt.legend(prop=my_font, loc='upper left')

# 添加描述信息
plt.xlabel('时间', fontproperties=my_font)
plt.ylabel('温度 单位(t)', fontproperties=my_font)
plt.title('3月份和10月份的温度变化散点图', fontproperties=my_font)

plt.show()
散点图实例1

3、条形图

# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午8:48
# @Author  : Felix Wang

import random
from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

a = ['电影{}'.format(i) for i in range(30)]  # 电影名称
b = [random.randint(10, 50) for i in range(30)]  # 电影票房

# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘制条形图
plt.bar(range(len(a)), b, width=0.3)
# 设置字符串到x轴
plt.xticks(range(len(a)), a, fontproperties=my_font, rotation=45)  # x轴对应
plt.savefig('电影统计.png')
plt.show()
实例1
# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午8:48
# @Author  : Felix Wang

# 绘制横着的条形图
import random
from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

a = ['电影{}'.format(i) for i in range(30)]  # 电影名称
b = [random.randint(10, 50) for i in range(30)]  # 电影票房

# 设置图形大小
plt.figure(figsize=(8, 20), dpi=80)
# 绘制横着的条形图
plt.barh(range(len(a)), b, height=0.3, color='orange')
# 设置字符串到x轴
plt.yticks(range(len(a)), a, fontproperties=my_font)  # x轴对应
# 添加网格
plt.grid(alpha=0.3)

plt.savefig('电影统计.png')
plt.show()
实例2
# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午9:06
# @Author  : Felix Wang

from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

a = ['猩球崛起', '敦刻尔克', '蜘蛛侠', '战狼2']
b_16 = [15746, 312, 4497, 319]
b_15 = [12357, 156, 2045, 168]
b_14 = [2358, 399, 2357, 362]

bar_width = 0.1
x_14 = list(range(len(a)))
x_15 = [i + bar_width for i in x_14]
x_16 = [i + bar_width for i in x_15]

plt.figure(dpi=80)

plt.barh(x_14, b_14, color='red', height=bar_width, label='9月14日')
plt.barh(x_15, b_15, color='yellow', height=bar_width, label='9月15日')
plt.barh(x_16, b_16, color='blue', height=bar_width, label='9月16日')

plt.yticks(x_15, a, fontproperties=my_font)
plt.legend(prop=my_font)  # 标签
plt.show()
实例3

4、直方图

# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午9:41
# @Author  : Felix Wang

import random
from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 一般来说能够使用直方图的都是那些没有统计过的数据

# 设置字体
my_font = font_manager.FontProperties(fname='/home/felix/.local/share/fonts/SIMHEI.TTF')

a = [random.randint(10, 100) for i in range(100)]
# 直方图组数计算方式:将数据分组,当数据在100个以内时,按数据多少常分5-12组
# 组距:指每个小组的两个端点的距离
# 组数:极差/组距
d = 5  # 组距
num_bins = (max(a) - min(a)) // d  # 注意组距一定要被numbins整除,否则会不均匀
print(num_bins)

plt.hist(a, num_bins)

# 设置x轴的刻度
plt.xticks(range(min(a), max(a) + d, d))
# 显示网格
plt.grid()
plt.show()
实例1
# -*- coding: utf-8 -*-
# @Time    : 18-12-11 下午10:15
# @Author  : Felix Wang

import random
from matplotlib import (
    pyplot as plt,
    font_manager,
)

# 直方图应用场景
# 1、用户的年龄分布状态
# 2、一段时间内用户的点击次数的分布状态
# 3、用户活跃时间分布状态

# 绘制不同刻度的x轴的直方图

interval = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 60, 90]
width = [5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 30, 60]
quantity = [random.randint(500, 9000) for i in range(len(interval))]

plt.figure(figsize=(20, 8), dpi=80)

plt.bar(range(len(quantity)), quantity, width=1)

# 设置x轴的刻度
_x = [i - 0.5 for i in range(len(quantity) + 1)]
_xtick_labels = interval + [150]
plt.xticks(_x, _xtick_labels)
plt.grid()

plt.show()
实例2

5、归纳

原文地址:https://www.cnblogs.com/felixwang2/p/10105966.html