matplotlib实例

matplotlib实例

matplotlib是一个Python的2D绘图库,通过Matplotlib,可以仅需几行代码,便可生成绘图,直方图,条形图,散点图等。
绘图步骤:

  1. 创建图纸(figure)
  2. 在图纸上创建一个或多个绘图(plot)区域(也叫子图,坐标系/轴,axis)
  3. 在plot区域上描绘点、线等各种marker
  4. 为plot添加修饰标签(绘图线上的或坐标轴上的)
  5. 其他各种DIY
# -*- coding:utf-8 -*-

from matplotlib import pyplot as plt


# figure图像图标的意思,在这里指的就是我们画的图
# fig = plt.figure(figsize=(20, 8), dpi=80)
fig = plt.figure(figsize=(10, 5))

# 数据在x轴的位置,是一个可迭代对象
x = range(2, 26, 2)

# 数据在y轴的位置,是一个可迭代对象
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]

plt.plot(x, y)
# 设置x刻度, 当刻度太密时,使用列表的步长(间隔取值),来解决,
# matplotlib会自动对应
# plt.xticks(x)
plt.xticks(x[::2])
# 保存图片
# plt.savefig("./sig_size.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt
import random

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

plt.figure(figsize=(20, 8))
x = range(120)

# 设置随机种子,让不同时候随机的得到的结果都一样
random.seed(10)
y = [random.uniform(20, 35) for i in range(120)]

plt.plot(x, y)

_x_ticks = ["10点{}分".format(i) for i in x if i < 60]
_x_ticks += ["11点{}分".format(i - 60) for i in x if i > 60]

plt.xticks(x[::5], _x_ticks[::5], rotation=90)

# 给图像添加描述信息
plt.xlabel("时间")  # 设置x轴的label
plt.ylabel("温度")  # 设置y轴的label
plt.title("10点到12点每分钟的时间变化情况")  # 设置title

# 保存图片
plt.savefig("./test09.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt

# 设置中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签

plt.figure(figsize=(20, 8))
x = range(11, 31)
y = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
z = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]

fig, ax = plt.subplots()
ax.plot(x, y, label='张三', color='r', linestyle='--', linewidth=5, alpha=0.5)
ax.plot(x, z, label='李四', color='orange', linestyle='-', linewidth=5, alpha=0.5)

# 右上角显示图标
plt.legend(loc='best')

x_ticks = ["{}岁".format(i) for i in x]

plt.xticks(x[::2], x_ticks[::2], rotation=0)

plt.xlabel("年龄")
plt.ylabel("数量")
plt.title("11岁到30岁每年交朋友的变化情况")

# 绘制网格
plt.grid(alpha=0.2)

# 加水印
fig.text(0.75, 0.45, '大数据', fontsize=40, color='gray', ha='right', va='bottom', alpha=0.2)

plt.savefig("./test10.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt

"""绘制散点图"""

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签

plt.figure(figsize=(20, 8), dpi=80)  # 显示图片大小

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]
x_3 = range(1, 32)  # 31天

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_10 = range(51, 82)  # 31天

plt.scatter(x_3, y_3, label='3月份')
plt.scatter(x_10, y_10, label='10月份')

x = list(x_3) + list(x_10)
x_ticks = ['3月{}日'.format(i) for i in x_3]
x_ticks += ['10月{}日'.format(i) for i in y_10]

plt.xticks(x[::3], x_ticks[::3], rotation=45)

plt.legend(loc='upper left')

plt.xlabel("时间")
plt.ylabel("温度")
plt.title("3月和10月温度对比情况")

plt.savefig("./test11.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt

# 中文+图片大小
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20, 8), dpi=80)

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

y = [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]

_x = range(len(x))

plt.bar(_x, y, width=0.2, color='orange')

plt.xticks(_x, x, rotation=90)

plt.xlabel("电影名称")
plt.ylabel("票房(亿)")
plt.title("2017年内地票房前20的电影情况")

plt.savefig("./test12.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt

# 中文+图片大小
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20, 8), dpi=80)

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

y = [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]

_x = range(len(x))

plt.barh(_x, y, height=0.2, color='orange')

plt.yticks(_x, x)

plt.ylabel("电影名称")
plt.xlabel("票房(亿)")
plt.title("2017年内地票房前20的电影情况")

plt.savefig("./test12_1.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt

# 中文+图片大小
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20, 8), dpi=80)

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

bar_with = 0.2
x = range(len(a))
x_14 = list(x)
x_15 = [i + bar_with for i in x_14]
x_16 = [i + bar_with * 2 for i in x_14]

plt.bar(x, b_14, width=bar_with, label='9月14')
plt.bar(x_15, b_15, width=bar_with, label='9月15')
plt.bar(x_16, b_16, width=bar_with, label='9月16')

plt.xticks(x_15, a, rotation=0)

plt.xlabel("电影")
plt.ylabel("票房(单位:万元)")
plt.title("9月14~16日电影票房情况")

plt.legend(loc='best')

plt.savefig("./test13.png")
plt.show()

matplotlib实例

from matplotlib import pyplot as plt

# 中文+图片大小
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20, 8), dpi=80)

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]

# 设置组距
bin_width = 3
# 分多少组
num_bins = (max(a) - min(a)) // bin_width

plt.hist(a, num_bins)
# plt.hist(a, num_bins, normed=True)

plt.xticks(range(min(a), max(a), bin_width))

plt.grid(alpha=0.5)

plt.xlabel("时间(单位:分钟)")
plt.ylabel("数量")
plt.title("250部电影时长分布")

plt.savefig("test14.png")
plt.show()

matplotlib实例
有关matplotlib更详细的教程,参考官方示例文档