用matplotlib画个图系列-折线图

  • 折线图使用场景
    折线图一般用于表现数据的时间序列变化,可以很明确的反映出数据的变化趋势。比如2010-2019年我国GDP的变化趋势就可以用折线图展示。
  • 折线图作图
    直接用line,关键在于线型、label的设置
  • 折线图作图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'SimHei'
label = ['经理级', '普通员工级']
ticks = [2015, 2016, 2017, 2018, 2019]
sale1 = [9500, 9800, 10000, 10500, 11000]
sale2 = [4200, 4600, 4800, 5200, 5500]
product1 = [8500, 8800, 9100, 9500, 10000]
product2 = [4050, 4300, 4800, 5200, 5400]
sale1_g = []
sale2_g = []
product1_g = []
product2_g = []
for i in range(len(sale1)):
    if i == 0:
        sale1_g.append('-')
    else:
        tmp = (sale1[i] - sale1[i-1])/sale1[i-1]
        tmps = '{:.1%}'.format(tmp)
        sale1_g.append(tmps)
for i in range(len(sale2)):
    if i == 0:
        sale2_g.append('-')
    else:
        tmp = (sale2[i] - sale2[i - 1]) / sale2[i - 1]
        tmps = '{:.1%}'.format(tmp)
        sale2_g.append(tmps)
for i in range(len(product1)):
    if i == 0:
        product1_g.append('-')
    else:
        tmp = (product1[i] - product1[i - 1]) / product1[i - 1]
        tmps = '{:.1%}'.format(tmp)
        product1_g.append(tmps)
for i in range(len(product2)):
    if i == 0:
        product2_g.append('-')
    else:
        tmp = (product2[i] - product2[i - 1]) / product2[i - 1]
        tmps = '{:.1%}'.format(tmp)
        product2_g.append(tmps)
print(sale1_g)
print(sale2_g)
print(product1_g)
print(product2_g)
ax1 = plt.subplot(121)
ax1.plot(sale1, color='b', linewidth=2, linestyle='-', marker='o', label='经理级')
for x, y in enumerate(sale1):
    ax1.text(x, y-500, sale1_g[x])
ax1.plot(sale2, color='r', linewidth=2, linestyle='-', marker='o', label='普通员工级')
for x, y in enumerate(sale2):
    ax1.text(x, y-500, sale2_g[x])
ax1.set_ylim(0, 12000)
ax1.set_xticks(range(len(ticks)))
ax1.set_xticklabels(ticks)
ax1.legend(loc='upper left')
ax1.set_title('采购销售部门工资变化')

ax2 = plt.subplot(122)
ax2.plot(product1, color='b', linewidth=2, linestyle='-', marker='o', label='经理级')
for x, y in enumerate(product1):
    ax2.text(x, y-500, product1_g[x])
ax2.plot(product2, color='r', linewidth=2, linestyle='-', marker='o', label='普通员工级')
for x, y in enumerate(product2):
    ax2.text(x, y-500, product2_g[x])
ax2.set_ylim(0, 12000)
ax2.set_xticks(range(len(ticks)))
ax2.set_xticklabels(ticks)
ax2.legend(loc='upper left')
ax2.set_title('生产加工部门工资变化')
plt.show()
  • 折线图展示


    折线图

你可能感兴趣的:(用matplotlib画个图系列-折线图)