Python 向绘制的直方图添加图例

Python 向绘制的直方图添加图例,python,matplotlib,histogram,Python,Matplotlib,Histogram,如何在柱状图打印后向柱状图添加标签 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

如何在柱状图打印后向柱状图添加标签

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()

您可以通过将字符串传递到
legend()


你所说的标签是什么意思?您希望该标签显示在哪里?为什么要在创建直方图后添加它?我想在绘图的图例中显示一个标签,我需要显示一个在绘制直方图后计算的值(该值取决于直方图分类,如我在示例中所示),简单如:O,谢谢。如果我想添加两个“MyLabel”,我该如何实现这一点?也必须很简单。你能试着把你面临的困难写出来吗?将其视为您的最后手段:
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()