简体   繁体   English

向绘制的直方图添加图例

[英]Adding legend to a plotted histogram

How can I a menage to add a label to a histogram, after its plotting?在绘制后,如何管理将标签添加到直方图?

import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
x = [2, 4, 6, 2, 4, 7, 6, 4, 4, 4, 4]

n, bins_edges, patches = ax1.hist(x, log=True, bins='doane', color="red")
binwidth =  bins_edges[1] - bins_edges[0]
mylabel = "Binwidth {}".format(binwidth)
ax1.hist[-1].set_label(mylabel)
plt.legend()
plt.show()

You can add string to legend by passing it to legend()您可以通过将字符串传递给legend()来将字符串添加到图例中

import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
x = [2, 4, 6, 2, 4, 7, 6, 4, 4, 4, 4]

n, bins_edges, patches = ax1.hist(x, log=True, bins='doane', color="red")
binwidth =  bins_edges[1] - bins_edges[0]
mylabel = "Binwidth {}".format(binwidth)


ax1.legend([mylabel])
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM