Добавление легенды к построенной гистограмме - PullRequest
0 голосов
/ 29 января 2020

Как мне добавить метку к гистограмме после ее построения?

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

1 Ответ

0 голосов
/ 29 января 2020

Вы можете добавить строку к легенде, передав ее 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()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...