我有以下代码:
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
hour_of_day = [17, 13, 21, 21, 16, 15, 9, 10, 12, 11, 10, 1, 5, 13, 13, 18, 21, 23, 23, 14, 11, 12, 15, 17, 9, 19, 7, 20, 20, 21, 21, 19, 2, 8, 23, 8, 8, 10, 15, 11, 19, 18, 20, 22, 22, 0, 0, 10, 13, 11, 16, 18, 18, 19, 19, 19, 23, 4, 6, 11, 15, 14, 16, 21, 20, 20, 21, 21, 14, 15, 19, 20, 23, 6, 6, 14, 15, 0, 1, 1, 12, 20, 21, 21, 3, 2, 5, 6, 6, 7, 10, 10, 11, 12, 12, 9, 14, 14, 19, 15, 16, 21, 21, 22, 22, 20, 4, 6, 7, 17, 17, 18, 18, 19, 8, 14, 13, 20, 14, 0, 6, 4, 7, 13, 12, 12, 17, 19, 21, 22, 0, 0]
fig, ax = plt.subplots()
counts, bins, patches = ax.hist(
hour_of_day,bins=range(25), normed = False, color = 'g')
plt.plot(bins)
plt.show()
它产生(几乎)预期的数字。特别是,它呈现出一个无法解释的蓝色(!)对角线。这条线是什么?它来自哪里?我该如何禁用它?
供参考,以下是我得到的结果:
答案 0 :(得分:1)
此处,您的直方图由ax.hist(
hour_of_day,bins=range(25), normed = False, color = 'g')
生成。
蓝色对角线是由对plt.plot(bins)
的调用引起的,这会产生bins
与x轴的x-y图(在这种情况下也是bins
)。您可以删除此行,不需要的蓝线也会消失。