Button press event - event handling and picking: IndexError: list index out of range

Caledonian26 caledonian209 at gmail.com
Sun Jun 7 15:06:05 EDT 2020


I have the following command below.

The overall aim is to allow the user to select a Y-value by pressing on the bar graph. The colour of each bar should then change depending on what this Y-value is.

import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import matplotlib.colors
np.random.seed(12345)

df = pd.DataFrame({"values":[np.random.normal(32000,200000,3650).mean(), 
                   np.random.normal(43000,100000,3650).mean(), 
                   np.random.normal(43500,140000,3650).mean(), 
                   np.random.normal(48000,70000,3650).mean()], 
                  "index":[1992,1993,1994,1995]})

standarderrors1992 = stats.sem(np.random.normal(32000,200000,3650))
standarderrors1993 = stats.sem(np.random.normal(43000,100000,3650))
standarderrors1994 = stats.sem(np.random.normal(43500,140000,3650))
standarderrors1995 = stats.sem(np.random.normal(48000,70000,3650)) 
add1992 = 1.96*standarderrors1992
add1993 = 1.96*standarderrors1993
add1994 = 1.96*standarderrors1994 
add1995 = 1.96*standarderrors1995
mean1992 = np.random.normal(32000,200000,3650).mean()
mean1993 = np.random.normal(43000,100000,3650).mean()
mean1994 = np.random.normal(43500,140000,3650).mean()
mean1995 = np.random.normal(48000,70000,3650).mean()
labels = [1992,1993,1994,1995]
add = [add1992,add1992,add1994,add1995]

1. This first part organises the raw data.

limits = []

def onclick(event):
    plt.cla()
    plt.bar(df["index"].values,df["values"].values,align='center', alpha=0.5,yerr=add)
    plt.xticks(labels)
    limit = event.ydata
    limits.append(limit)
    if len(limits) >= 1:
        plt.gcf().canvas.mpl_disconnect(plt.gcf().canvas.mpl_connect('button_press_event', onclick))

plt.gcf().canvas.mpl_connect('button_press_event', onclick)

2. This next part allows the user to press on the graph to select a Y value. This should be assigned to the variable 'limits'

 dict = {mean1992:add1992,mean1993:add1993,mean1994:add1994,mean1995:add1995}
  colourofbars = []
  for key,value in dict.items():
      if limits[0] > (key+(value)):
          colour = 1 
          colourofbars.append(colour)
      elif limits[0] < (key-(value)):
          colour = 0 
          colourofbars.append(colour)
      elif (limits[0] < (key+(value))) & (limits[0] > (key-(value))): 
          colour = ((key+(value))-limits[0])/((key+value)-(key-value)) 
          colourofbars.append(colour)
df["colourofbars"] = colourofbars

3. Here, the list 'colourofbars' is appended based on the data above, and added as a column to the dataframe 'df'.

cmap = plt.cm.rainbow
norm = matplotlib.colors.Normalize(vmin=1.5, vmax=4.5)
plt.bar(df["index"].values,df["values"].values,color=cmap(norm(df["colourofbars"].values)),align='center', alpha=0.5,yerr=add)
plt.xticks(labels)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
plt.gcf().colorbar(sm)
plt.show()

4. Here, a different colour is assigned to each bar in the bar chart depending on the values in the column 'colourofbars'. I then try to plot a legend showing this colour gradient scale.

However, I keep getting the error: IndexError: list index out of range. Could anyone give me a helping hand as to where I am going wrong?


More information about the Python-list mailing list