3

How would I hide the output of plt.hist() when also trying to plot its top curve?

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

enter image description here

similar question, but I can't just clear the plot since there's a loop. Hide histogram plot

Community
  • 1
  • 1
waspinator
  • 6,464
  • 11
  • 52
  • 78

2 Answers2

5

One possible way of obtaining slices of apples is of course to prepare an apple pie and later pick all the apples from the pie. The easier method would surely be not to make the cake at all.

So, the obvious way not to have a histogram plot in the figure is not to plot it in the first place. Instead calculate the histogram using numpy.histogram (which is anyways the function called by plt.hist), and plot its output to the figure.

import numpy as np
import matplotlib.pyplot as plt

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 = np.histogram(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()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

One of the methods is to set alpha to zero:

y_values, bin_edges, _ = plt.hist(values, bins=20, alpha = 0)

The second is to deny filling of bars and set line width to zero:

y_values, bin_edges, _ = plt.hist(values, bins=20, fill=False, linewidth=0)

enter image description here

Serenity
  • 35,289
  • 20
  • 120
  • 115