记录自己使用jupyter notebook来操作matplolib库来绘制折线图
1、简易的折线图
import matplotlib.pyplot as plt
import random as ran
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams["axes.unicode_minus"] = False
plt.figure()
plt.plot([1,2,3,4,5,6,7],[14,7,32,12,22,11,4])
plt.show()
效果如图下:
2、复杂点的折线图
import matplotlib.pyplot as plt
import random as ran
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams["axes.unicode_minus"] = False
#准备x,y轴坐标
x = range(60)
#补充一下该处可以只放一条,这样子就只绘制一条折线图
y_shanghai = [ran.uniform(15,18) for i in x]
y_beijing = [ran.uniform(12,29) for i in x]
plt.figure(figsize=(20,8),dpi=80)
# 绘制线条
plt.plot(x,y_shanghai,color='r',linestyle='--',label="上海")
plt.plot(x,y_beijing,color='g',linestyle='-',label="北京")
x_tickes_label = ["11点{}分".format(i)for i in x]
y_tickes_label = range(40)
plt.xticks(x[::5],x_tickes_label[::5])
plt.yticks(y_tickes_label[::5])
plt.grid(True,linestyle="--",alpha=1)
plt.xlabel("时间",fontsize=20)
plt.ylabel("温度",fontsize=20)
plt.title("中午11点-12点某城市温度变化图",fontsize=20)
# 显示图例
plt.legend(loc="best")
plt.savefig("./test.png")
plt.show()
效果如下图: