import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
xg_train_rmse_list = [314.32038804079116, 359.48599155662106, 205.5291460083981, 1469.4275722910766, 1829.7459800145982, 1327.82600487832, 165.78998330706915, 878.8991453525301, 438.1595281182948, 514.687740497555, 190.5174103985512, 322.14463546272805, 370.0971890574493, 492.1098778216327, 306.019471382785, 483.66994547637813, 141.41633288280156, 351.8569077165809, 881.4664545258521, 433.1990986950801, 467.525447517998, 285.1966191346068, 774.0922517630846, 645.8192429703066] # y轴坐标
xg_test_rmse_list = [963.7157494183476, 1639.3228408331952, 906.2880767833137, 1827.1669793693875, 4175.067339011675, 1485.6009662103954, 5042.873656719206, 4501.7091193842125, 3265.8542820894613, 3774.2053825601033, 1483.6199799326128, 8119.025306707887, 3177.687514554185, 6433.794844490714, 5981.565772885096, 6047.585822926207, 1690.1535110689003, 2440.1155538420726, 6185.380408488843, 3978.8710952890597, 2750.0972087356713, 3406.4256179550994, 4815.650980470394, 2961.257202628438] # y轴坐标
plt.xlabel('小时') # x轴坐标轴标题
plt.ylabel('RMSE') # y轴坐标轴标题
plt.title("网卡模型RMSE统计") # 图表标题
x = list(range(0, 24)) # x轴坐标
plt.plot(x, xg_train_rmse_list, 'r--', label='训练集RMSE') # 前两个参数是(x,y)坐标;'r--'是线条样式,r是红色,'--'代表虚线;label是该线的图例
plt.plot(x, xg_test_rmse_list, 'b-', label='测试集RMSE')
plt.legend(loc='best') # 图例的显示位置
for x, y in zip(x, xg_train_rmse_list): # 显示数据标签
plt.text(x, y+0.3, '%.0f'%y, ha='center', va='bottom', fontsize=10.5)
x = list(range(0, 24))
for x, y in zip(x, xg_test_rmse_list):
plt.text(x, y+0.3, '%.0f'%y, ha='center', va='bottom', fontsize=10.5)
# plt.annotate('s', xy=(1, 0), xycoords='data',textcoords='offset points', fontsize=16,
# arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) # 添加注解
plt.show()
效果图如下:
参考:
PYTHON 画图神器 Matplotlib
Python学习笔记(4)——Matplotlib中的annotate(注解)的用法
在 python 中用 matplotlib 绘图并添加数据标签