python · matplotlib | seaborn 画图与调整图例位置


1 seaborn 画图代码存档:

sns.set_style("whitegrid") # 好看的 style
plt.figure()
# plt.plot(ppo_data['Step'] * step_mul, ppo_data['ppo_mean'], label='PPO')
# plt.plot(sac_data['Step'] * step_mul, sac_data['sac_mean'], label='SAC (continuous)')
# plt.plot(base_data['Step'] * step_mul, base_data['group_sac_mean'] * 6, label='Base Policy')
# plt.plot((finetune_data['Step'] + 2000) * step_mul, finetune_data['finetune_sac_mean'] * 6, label='Fine-tuned Policy')
sns.lineplot(data=ppo_new, x='Step', y='ppo_mean', label='PPO') # 画出带阴影的 fancy RL curve
sns.lineplot(data=sac_new, x='Step', y='sac_mean', label='SAC (continuous)')
sns.lineplot(data=base_new, x='Step', y='group_sac_mean', label='Base Policy')
sns.lineplot(data=finetune_new, x='Step', y='finetune_sac_mean', label='Fine-tuned Policy')
plt.xlabel('Step')
plt.ylabel('Reward')
plt.legend()
plt.show()

2 调整 matplotlib 的图例位置(图外 正下方 2×2 平铺):

# 画图
sns.set_style("whitegrid")
plt.figure()
# base policy 的 rack out 采用红色虚线
plt.plot(base_data['step'], base_data['rack_out'], color='indianred', ls='-.', label='Rack Outlet Temperature \nof Base Policy')
# base policy 的 setpoint 采用红色实线
plt.plot(base_data['step'], base_data['setpoint'], color='brown', label='ACU Setpoints \nof Base Policy')
# fine-tuned policy 的 rack out 采用蓝色虚线
plt.plot(finetune_data['step'], finetune_data['rack_out'], color='skyblue', ls='-.', label='Rack Outlet Temperature \nof Fine-tuned Policy')
# fine-tuned policy 的 setpoint 采用蓝色实线
plt.plot(finetune_data['step'], finetune_data['setpoint'], color='tab:blue', label='ACU Setpoints \nof Fine-tuned Policy')
plt.xlabel('Step')
plt.ylabel('Temperature (°C)')
plt.legend(bbox_to_anchor=(0.5,-0.3), loc='center', ncol=2)
plt.tight_layout()
plt.show()

在 plt.legend 中调整图例位置,

  1. 把图例放在图外面(并更改位置(?)):bbox_to_anchor=(0.5,-0.3)
  2. 更改对齐方式:loc='center'
  3. 更改一行放几个图例:ncol=2

然后,调用 plt.tight_layout() ,否则图片显示不完全。

3 matplotlib 的 Type-3 字体问题

参考博客:https://blog.csdn.net/qq_45909595/article/details/136284849

import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42


posted @ 2024-02-20 11:29  MoonOut  阅读(405)  评论(0编辑  收藏  举报