隐藏 matplotlib 的直方图输出

was*_*tor 3 python matplotlib histogram

plt.hist()当还试图绘制其顶部曲线时,我将如何隐藏输出?

data = {'first': np.random.normal(size=[100]),
        'second': np.random.normal(size=[100]),
        'third': np.random.normal(size=[100])}

for data_set, values in sorted(data.items()):
    y_values, bin_edges, _ = plt.hist(values, bins=20)
    bin_centers = 0.5*(bin_edges[1:] + bin_edges[:-1])
    plt.plot(bin_centers, y_values, '-', label=data_set)

legend = plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

类似的问题,但我不能只是清除情节,因为有一个循环。 隐藏直方图

Imp*_*est 5

获得苹果片的一种可能方法当然是准备一个苹果派,然后从派中挑选所有的苹果。更简单的方法肯定是根本不做蛋糕。

因此,在图中没有直方图的明显方法是首先不要绘制它。而是使用numpy.histogram(无论如何都是由 调用plt.hist函数)计算直方图,并将其输出绘制到图形中。

将 numpy 导入为 np
导入 matplotlib.pyplot 作为 plt

data = {'first': np.random.normal(size=[100]),
        '第二':np.random.normal(size=[100]),
        '第三':np.random.normal(size=[100])}

对于 data_set,排序后的值(data.items()):
    y_values, bin_edges = np.histogram(values, bins=20)
    bin_centers = 0.5*(bin_edges[1:] + bin_edges[:-1])
    plt.plot(bin_centers, y_values, '-', label=data_set)

图例 = plt.legend()
plt.show()

在此处输入图片说明