漂亮的条形图,均值和变异系数

时间:2019-04-12 11:27:29

标签: python matplotlib

我正在绘制一些具有均值和均值变异系数的值。事实是,我不知道如何将这两个值都放在情节上,并且看起来还不错。我的方法是这样的:

import matplotlib.pyplot as plt
import numpy as np
colors = ["b", "g", "r", "c", "m", "y", "k", "w"]
models = ["DQN", "DDQN", "DoubleDQN", "DoubleDDQN", "RND", "DQNfD"]
means = [1.90, 0.67, 1.32, 2.02, 0.90, 1.92]
cvs =   [1.34, 2.32, 1.44, 1.32, 2.03, 1.33]
cont = 0
for m, c, mean, cv in zip(models, colors, means, cvs):
    plt.bar(cont, mean, label = "CV = {:.2f}".format(cv), color = c)
    plt.text(cont-0.16, mean + 0.03, "{:.2f}".format(mean))
    plt.title("Mean Episode Reward at Test")
    plt.ylabel('Mean Episode Reward')
    plt.xticks(np.arange(len(models)), models)
    cont+=1
plt.legend()
plt.tight_layout()

输出是这样的: 1 我想以图形和数字方式查看均值和cvs的值,但是我不知道该怎么做(如果cv无法实现,请不要介意)。简历的误差线不是最好的选择,因为我们的规模不一样,但在图例中显示误差线却很丑。

1 个答案:

答案 0 :(得分:1)

我必须承认我不确定期望的结果到底是什么;所以这只是一个美化建议:

import matplotlib.pyplot as plt

colors = ["b", "g", "r", "c", "m", "y", "k", "w"]
models = ["DQN", "DDQN", "DoubleDQN", "DoubleDDQN", "RND", "DQNfD"]
means = [1.90, 0.67, 1.32, 2.02, 0.90, 1.92]
cvs =   [1.34, 2.32, 1.44, 1.32, 2.03, 1.33]


plt.bar(models, means, color=colors[:len(means)])

for i, (mean, cv) in enumerate(zip(means, cvs)):
    annotkw = dict(textcoords="offset points", ha="center")
    plt.annotate("CV = {:.2f}".format(cv), xy=(i, mean), xytext=(0, -3),
                 va = "top", fontsize=8, fontweight="bold",
                 color="w", **annotkw)
    plt.annotate("{:.2f}".format(mean), xy=(i, mean), xytext=(0, 1),
                 va = "bottom", **annotkw)

plt.title("Mean Episode Reward at Test")
plt.ylabel('Mean Episode Reward')

plt.margins(y=0.1)
plt.tight_layout()
plt.show()

enter image description here