# 强化学习实验绘图（使用seaborn）

### 文章目录

seaborn可以说是matplotlib的升级版，使用seaborn绘制折线图时参数数据可以传递ndarray或者pandas。

#### 1.从一个演示示例开始

##### 1.1 极简示例
``````import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns # 导入模块
sns.set() # 设置美化参数，一般默认就好

rewards = np.array([0, 0.1,0,0.2,0.4,0.5,0.6,0.9,0.9,0.9])
plt.plot(rewards)
plt.show()
``````

##### 1.2 使用sns.lineplot

``````import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns;
sns.set() # 因为sns.set()一般不用改，可以在导入模块时顺便设置好

rewards = np.array([0, 0.1,0,0.2,0.4,0.5,0.6,0.9,0.9,0.9])
sns.lineplot(x=range(len(rewards)),y=rewards)
# sns.relplot(x=range(len(rewards)),y=rewards,kind="line") # 与上面一行等价
plt.xlabel("episode")
plt.ylabel("reward")
plt.title("data")
plt.show()
``````

##### 1.3 绘制rewards聚合图

``````import numpy as np

rewards1 = np.array([0, 0.1,0,0.2,0.4,0.5,0.6,0.9,0.9,0.9])
rewards2 = np.array([0, 0,0.1,0.4,0.5,0.5,0.55,0.8,0.9,1])
rewards3 = np.vstack((rewards1,rewards2)) # 合并成二维数组
rewards4 = np.concatenate((rewards1,rewards2)) # 合并成一维数组
print(np.shape(rewards3))
print(rewards3)
print(np.shape(rewards4))
print(rewards4)
``````

``````import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns;
sns.set() # 因为sns.set()一般不用改，可以在导入模块时顺便设置好

rewards1 = np.array([0, 0.1,0,0.2,0.4,0.5,0.6,0.9,0.9,0.9])
rewards2 = np.array([0, 0,0.1,0.4,0.5,0.5,0.55,0.8,0.9,1])
rewards=np.concatenate((rewards1,rewards2)) # 合并数组
episode1=range(len(rewards1))
episode2=range(len(rewards2))
episode=np.concatenate((episode1,episode2))
sns.lineplot(x=episode,y=rewards)
plt.xlabel("episode")
plt.ylabel("reward")
plt.show()
``````

##### 1.4 使用pandas传参

``````import numpy as np
import pandas as pd
rewards1 = np.array([0, 0.1,0,0.2,0.4,0.5,0.6,0.9,0.9,0.9])
rewards2 = np.array([0, 0,0.1,0.4,0.5,0.5,0.55,0.8,0.9,1])
rewards=np.vstack((rewards1,rewards2)) # 合并数组
df = pd.DataFrame(rewards).melt(var_name='episode',value_name='reward') # 推荐这种转换方法
print(df)
``````

``````import seaborn as sns
sns.set()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

rewards1 = np.array([0, 0.1,0,0.2,0.4,0.5,0.6,0.9,0.9,0.9])
rewards2 = np.array([0, 0,0.1,0.4,0.5,0.5,0.55,0.8,0.9,1])
rewards=np.vstack((rewards1,rewards2)) # 合并为二维数组
df = pd.DataFrame(rewards).melt(var_name='episode',value_name='reward')

sns.lineplot(x="episode", y="reward", data=df)
plt.show()
``````

##### 1.5 一个稍微复杂的示例
``````import seaborn as sns
sns.set()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def get_data():
'''获取数据
'''
basecond = np.array([[18, 20, 19, 18, 13, 4, 1],[20, 17, 12, 9, 3, 0, 0],[20, 20, 20, 12, 5, 3, 0]])
cond1 = np.array([[18, 19, 18, 19, 20, 15, 14],[19, 20, 18, 16, 20, 15, 9],[19, 20, 20, 20, 17, 10, 0]])
cond2 = np.array([[20, 20, 20, 20, 19, 17, 4],[20, 20, 20, 20, 20, 19, 7],[19, 20, 20, 19, 19, 15, 2]])
cond3 = np.array([[20, 20, 20, 20, 19, 17, 12],[18, 20, 19, 18, 13, 4, 1], [20, 19, 18, 17, 13, 2, 0]])
return basecond, cond1, cond2, cond3

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) # 合并
print(df)
sns.lineplot(x="episode", y="loss", hue="algo", style="algo",data=df)
plt.title("some loss")
plt.show()
``````

#### 2.读取csv文件并绘图

kaggle上一个酒店房间预定的数据，数据和本篇文章的代码都可以从这个链接获取：https://www.jianguoyun.com/p/Ddc6RhEQnNm0CRjc2aAE。

##### 2.1 简单示例

``````import pandas as pd
``````

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns # 导入模块
sns.set() # 设置美化参数，一般默认就好
sns.lineplot(x="arrival_date_month",y="stays_in_week_nights",data=df)
plt.show()
``````

##### 2.2 复杂示例

``````import pandas as pd
df=df[['arrival_date_year','arrival_date_month','stays_in_week_nights']]
print(df)
``````

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns # 导入模块
sns.set() # 设置美化参数，一般默认就好

df=df[['arrival_date_year','arrival_date_month','stays_in_week_nights']]
# order=df['arrival_date_month']

df_wide=df.pivot_table(index='arrival_date_month',columns='arrival_date_year',values='stays_in_week_nights')
print(df_wide)
sns.lineplot(data=df_wide)
plt.show()
``````

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns # 导入模块
sns.set() # 设置美化参数，一般默认就好

df=df[['arrival_date_year','arrival_date_month','stays_in_week_nights']]
order=df['arrival_date_month']

df_wide=df.pivot_table(index='arrival_date_month',columns='arrival_date_year',values='stays_in_week_nights')

df_wide=df_wide.reindex(order,axis=0)
print(df_wide)
sns.lineplot(data=df_wide)
plt.show()
``````

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns # 导入模块
sns.set() # 设置美化参数，一般默认就好
sns.lineplot(x="arrival_date_month",y="stays_in_week_nights",hue="arrival_date_year",data=df)
plt.show()
``````

https://zhuanlan.zhihu.com/p/147847062

https://www.guyuehome.com/36179

THE END