1.Seaborn介绍
matplotlib是python最常见的绘图包,强大之处不言而喻。然而在数据科学领域,可视化库Seaborn也是重量级的存在。由于matplotlib比较底层,想要绘制漂亮的图非常麻烦,需要写大量的代码。
Seaborn是在matplotlib基础上进行了高级API封装,图表装饰更加容易,你可以用更少的代码做出更美观的图。同时,Seaborn高度兼容了numy、pandas、scipy等库,使得数据可视化更加方便快捷。
2.Seaborn绘图代码
代码:
import seaborn as sns; sns.set() import matplotlib.pyplot as plt import numpy as np import pandas as pd def get_data(): ''' 获取数据 ''' baseline1 = np.array([[18, 20, 19, 18, 13, 4, 1],[20, 17, 12, 9, 3, 0, 0],[20, 20, 20, 12, 5, 3, 0]]) algorithm1 = np.array([[18, 19, 18, 19, 20, 15, 14],[19, 20, 18, 16, 20, 15, 9],[19, 20, 20, 20, 17, 10, 0]]) algorithm2 = np.array([[20, 20, 20, 20, 19, 17, 4],[20, 20, 20, 20, 20, 19, 7],[19, 20, 20, 19, 19, 15, 2]]) algorithm3 = np.array([[20, 20, 20, 20, 19, 17, 12],[18, 20, 19, 18, 13, 4, 1], [20, 19, 18, 17, 13, 2, 0]]) return baseline1, algorithm1, algorithm2, algorithm3 data = get_data() label = ['algo1', 'algo2', 'algo3', 'algo4'] df=[] for i in range(len(data)): df.append(pd.DataFrame(data[i]).melt(var_name='episode',value_name='loss')) df[i]['algo']= label[i] df=pd.concat(df) # 合并 plt.figure(figsize=(10, 6)) sns.lineplot(x="episode", y="loss", hue="algo", style="algo",data=df) plt.title("algorithm loss") plt.show()
绘图结果: