Tiny error in matplotlib gallery

Hello everyone,

I am not sure if this is the right place to post this, if not please kindly guide me to the right place. Thanks :slight_smile:

By accident I found a small error on this page:


In the code example, the line

centers = 0.5 * (bin_edges + np.roll(bin_edges, 1))[:-1]

should actually be one of the following two:

centers = 0.5 * (bin_edges + np.roll(bin_edges, -1))[:-1]
centers = 0.5 * (bin_edges + np.roll(bin_edges, 1))[1:]

Explanation: bin_edges contains the edges of the bins. It is a linspace, which is basically a range(a,b,c), for instance [0,1,2,3,4]. The centers of the bars corresponding to the bins should be the means of two adjacent values, in this example [0.5, 1.5, 2.5, 3.5]. However, the expression in the current code gives [2.0, 0.5, 1.5, 2.5], because the “direction of np.roll” and the “side of trimming” do not align. This is why either of my corrections works.

Interestingly, this is why we get the “lonely” upper green block in the output plot which is not present when inspecting the corresponding numpy array in binned_data_sets: it actually belongs at the bottom.

What steps can I take to further help getting this fixed?


Thanks for the note; I’ve fixed in a slightly different way here:

1 Like

Sorry, I missed that you want to try and help fix it yourself. If you find any additional bugs, please see our contribution guidelines.

1 Like

Hey @quantum.analyst !

Thanks for handling this for me :slight_smile: That’s totally fine with me, I just didn’t want to “off-load” this somewhere and expect others to handle it.

And your solution is surely easier to understand :innocent: I don’t think I’m able to judge which of them would be faster or more idiomatic or anything. Not that speed matters for this example anyway.

Finally thanks for pointing me to the contribution guidelines! If I find anything else, I will follow them.