1.matplotlib绘制两点直线
import matplotlib.pyplot as plt
plt.plot([0,2],[1,4])
plt.show()
2.matplotlib绘制折线图
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
squares = [1,15,9,16,25]
plt.plot(x,squares, linewidth=5)
plt.title('Number', fontsize=24)
plt.xlabel('datas', fontsize=14)
plt.ylabel('squares', fontsize=14)
plt.show()
3.matplotlib设置风格
3.1 matplotlib设置中文方法
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
squares = [1,15,9,16,25]
plt.plot(x,squares, linewidth=5)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('设置中文', fontsize=24)
plt.xlabel('datas', fontsize=14)
plt.ylabel('squares', fontsize=14)
plt.show()
3.2 matplotlib设置风格
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
print(plt.style.available)
plt.style.use('seaborn-deep')
x = np.linspace(-10,10)
plt.plot(x, np.sin(x))
4.在一张图中绘制多条折线图
max_tem = [26,30,31,32,33]
min_tem = [12,16,16,17,18]
x = range(5)
plt.rcParams['font.family'] = ['SimHei']
x_ticks = [f'星期{i}'.format(i) for i in range(1,6)]
plt.title('某年某月第几N周的温度')
plt.xlabel('周')
plt.ylabel('温度')
plt.xticks(x,x_ticks)
plt.plot(x, max_tem, label='最高温')
plt.plot(x, min_tem, label='最低温')
plt.legend(loc=2)