Blink
Blink

Reputation: 1504

pyplot.hist histtype = 'step' malfunction depending on data

I was doing a lot of plotting and ran into a weird anomaly with the pyplot.hist function. I've trimmed my program down to the minimum working example to show the problem.

import matplotlib.pyplot as plt
import numpy as np
var = [25.00, 35.68, 29.02, 25.46, 30.58, 30.86, 38.08, 38.63,
       25.19, 32.11, 26.57, 37.23, 37.97, 27.38, 27.25, 33.33,
       31.41, 26.93, 28.42, 25.99, 30.09, 31.87, 34.40, 33.46,
       31.76, 34.03, 27.01, 27.52, 30.41, 25.84, 25.84]
fig = plt.figure()
plt.hist(var, histtype = 'step')
plt.show()

When I run the script, I get this:

Anomaly http://dl.dropbox.com/u/13695305/Figure%201_005.png

However, if I remove the last 5 elements: 27.01, 27.52, 30.41, 25.84, 25.84, then the script works fine.

import matplotlib.pyplot as plt
import numpy as np
var = [25.00, 35.68, 29.02, 25.46, 30.58, 30.86, 38.08, 38.63,
       25.19, 32.11, 26.57, 37.23, 37.97, 27.38, 27.25, 33.33,
       31.41, 26.93, 28.42, 25.99, 30.09, 31.87, 34.40, 33.46,
       31.76, 34.03]
fig = plt.figure()
plt.hist(var, histtype = 'step')
plt.show()

Working! http://dl.dropbox.com/u/13695305/Figure%201_004.png

This is driving me crazy! I've tried using numpy arrays, and that didn't help. Numpy random worked no problem. But for some reason, my specific data set (type?) causes it to fail. Does anyone have any idea why?

Edit: It is important to note that the function works fine with histtype = 'bar', but the bug arises with histtype = 'step'. Hopefully someone can recreate this problem.

Upvotes: 1

Views: 1026

Answers (1)

seth
seth

Reputation: 1788

Unfortunately it's a bug, but there's a fix: Manually set the ymin for your plot.

import matplotlib.pyplot as plt
import numpy as np

var = [[25.00, 35.68, 29.02, 25.46, 30.58, 30.86, 38.08, 38.63,
        25.19, 32.11, 26.57, 37.23, 37.97, 27.38, 27.25, 33.33,
        31.41, 26.93, 28.42, 25.99, 30.09, 31.87, 34.40, 33.46,
        31.76, 34.03, 27.01, 27.52, 30.41, 25.84, 25.84]]
fig, ax = plt.subplots()#figsize = (10, 10))
ax.hist(var[0],histtype = 'step')
plt.ylim(ymin=0)
plt.show()

The bug seems to be fixed for v1.3, however. Apparently I'm using an earlier version.

>>> matplotlib.__version__
'1.1.1rc'

So upgrading matplotlib will probably fix the problem.

Upvotes: 1

Related Questions