Dave'o-Telephone
Dave'o-Telephone

Reputation: 113

Histogram problems in Python

I have an excel sheet containing three things I want to plot from three different times (24, 48 and 72h):

test = pd.read_excel(excel_sheet_location)
times = ('24h', '48h', '72h')

and a sample of the data look like this:

thing1  thing2 Time
38.655  8.655  24h     
18.385  8.655  24h     
26.013  8.655  24h     ...

So I'm trying to make 3 histograms for each time-point with the following code:

for i in range(2):
    fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(8,3))

    idx = (test["Time"] == t)

    ax[i].hist(test.ix[idx,'thing1'],range(51),normed=True,linewidth=0)

however, it draws the first histogram and then throws up these errors:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3113)()
pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2844)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()
pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7224)()
pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7162)()

KeyError: 0

Anyone got any ideas as to why it's doing this? The actual data is fine (i've checked by swapping round the values in the sheet and the same error occurs).

Upvotes: 0

Views: 52

Answers (1)

MSeifert
MSeifert

Reputation: 152677

Not sure if I can follow your error (maybe you just need to update to a newer version of pandas) or you do something strange to get from times to t.

If I use:

times = ['24h','48h']

# Create the figure outside the loop
fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(8,3))

# If you want 3 plots you need to change this to range(3) and the subplots above to (1, 3, ...)
for i in range(2):
    # Index the times list instead of the ominous t variable
    idx = (test["Time"] == times[i])
    ax[i].hist(test.ix[idx,'thing1'], range(51), normed=True, linewidth=0)

plt.show()

it works (just some random data):

enter image description here

Upvotes: 1

Related Questions