2

Passing a 2D array to Matplotlib's histogram function with histtype='step' seems to plot the columns in reverse order (at least from my biased, Western perspective of left-to-right).

Here's an illustration:

import matplotlib.pyplot as plt
import numpy as np

X = np.array([
    np.random.normal(size=5000),
    np.random.uniform(size=5000)*2.0 - 1.0,
    np.random.beta(2.0,1.0,size=5000)*3.0,
]).T

trash = plt.hist(X,bins=50,histtype='step')
plt.legend(['Normal','2*Uniform-1','3*Beta(2,1)'],loc='upper left')

Produces this:

enter image description here

Running matplotlib version 2.0.2, python 2.7

DavidG
  • 24,279
  • 14
  • 89
  • 82

1 Answers1

5

From the documentation for legend:

in order to keep the "label" and the legend element instance together, it is preferable to specify the label either at artist creation, or by calling the set_label method on the artist

I recommend to use the label keyword argument to hist:

String, or sequence of strings to match multiple datasets

The result is:

import matplotlib.pyplot as plt
import numpy as np

X = np.array([
    np.random.normal(size=5000),
    np.random.uniform(size=5000)*2.0 - 1.0,
    np.random.beta(2.0,1.0,size=5000)*3.0,
]).T

trash = plt.hist(X,bins=50,histtype='step',
                 label=['Normal','2*Uniform-1','3*Beta(2,1)'])
plt.legend(loc='upper left')
plt.show()
Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22