簡體   English   中英

向繪制的直方圖添加圖例

[英]Adding legend to a plotted 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 {}".format(binwidth)
ax1.hist[-1].set_label(mylabel)
plt.legend()
plt.show()

您可以通過將字符串傳遞給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