1. seaborn
旧版本(0.8.1)中使用tsplot,新版本中使用lineplot
直线代表均值,阴影代表mean±std(带有置信区间,参数ci)
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
sns.set()def smooth(data, wd=2):""":param data: ndarray,一维或二维:param wd::return:"""if not (isinstance(wd, int) and wd > 0):raise ValueError('wd must be a positive integer')elif 1 == wd:return dataelse:weight = np.ones(wd) / wdif 1 == data.ndim:return np.convolve(weight, data, "same")elif 2 == data.ndim:smooth_data = []for d in data:d = np.convolve(weight, d, "same")smooth_data.append(d)return np.array(smooth_data)else:raise ValueError('data must be a one-dimensional or two-dimensional ndarray')def get_data():returns1 = np.random.random((4, 100)) # 算法1,四个随机种子returns2 = np.random.random((4, 100)) + 1returns3 = np.random.random((4, 100)) + 2returns1 = smooth(returns1, 2)returns2 = smooth(returns2, 2)returns3 = smooth(returns3, 2)return returns1, returns2, returns3np.random.seed(11)
data = get_data()
label = ['algo1', 'algo2', 'algo3']
df=[]
ax = range(10, 100+10) # x轴刻度
for i in range(len(data)):df.append(pd.DataFrame(data[i], columns=ax).melt(var_name='episode',value_name='return'))df[i]['algo'] = label[i]
df=pd.concat(df, ignore_index=True)
# print(df)
sns.lineplot(x="episode", y="return", hue="algo", data=df)
plt.legend(loc='upper right')
# 'best', 'upper right', 'upper left', 'lower left', 'lower right',
# 'right', 'center left', 'center , right', 'lower center', 'upper center', 'center')
plt.title("")
plt.show()#
# import seaborn as sns
# import matplotlib.pyplot as plt
# fmri = sns.load_dataset("fmri")
# fmri.head()
#
# sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event")
# plt.show()
不进行平滑处理
平滑处理
matplotlib
画mean+/- standard deviation(std)的曲线图。
-
导入需要的库:matplotlib
-
用matplotlib.pyplot画均值曲线(图里的实线)
-
根据方差,用“fill_between”命令设定要填充曲线的上下限
-
用“fill”命令填充(图里的阴影部分)
(曲线颜色及线条粗细,填充颜色以及透明度都是在命令名后的括号里定义)
展示的结果图里画了三组数据,每组数据画法相同