Python – matplotlib – calling a function within an onClick() function

  matplotlib, pandas, python-3.x

Here is my code:

# Use the following data for this assignment:
%matplotlib notebook
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as mcol
import matplotlib.cm as cm
from matplotlib import colors
from scipy import stats

np.random.seed(12345)
xvals = [1992,1993,1994,1995]
df = pd.DataFrame([np.random.normal(32000,200000,3650), 
                   np.random.normal(43000,100000,3650), 
                   np.random.normal(43500,140000,3650), 
                   np.random.normal(48000,70000,3650)], 
                  index=xvals)
df['mean'] = df.mean(axis=1)  #create mean value column in DF
df['std'] = df.std(axis=1)
n= df.shape[1]
df['yerr'] = df['std'] / np.sqrt(n) * stats.t.ppf(1-0.05/2, n - 1)

def colorSelect():
    barColors=[]
    for row in range(len(df)):
        lowerVal = df.loc[df.index[row],'mean']-df.loc[df.index[row],'yerr']
        upperVal = df.loc[df.index[row],'mean']+df.loc[df.index[row],'yerr']
        meanVal = df.loc[df.index[row],'mean']
        if yval<lowerVal:
            if yval<0.9*lowerVal:
                barColors.append('darkred')
            elif yval<0.95*lowerVal:
                barColors.append('firebrick')
            else:
                barColors.append('red')
        elif yval>upperVal:
            if yval>1.1*upperVal:
                barColors.append('darkblue')
            elif yval>1.05*upperVal:
                barColors.append('blue')
            else:
                barColors.append('royalblue')
        elif yval<meanVal:
            if meanVal-yval>1000:
                barColors.append('salmon')
            else:
                barColors.append('white')
        elif yval>meanVal:
            if yval-meanVal>1000:
                barColors.append('cornflowerblue')
            else:
                barColors.append('white')
    return(barColors)
fig = plt.figure()
def updateChart():
    plt.clf()
    plt.bar(xvals, df['mean'], tick_label=[1992,1993,1994,1995],yerr=df['yerr'], color=colorSelect(), edgecolor='black') #plot bar chart
    plt.tick_params(bottom=False) #remove x ticks
    plt.axhline(yval, color='black')
    ax2 = plt.gca()
    right_side = ax2.spines["right"]
    right_side.set_visible(False)
    top_side = ax2.spines["top"]
    top_side.set_visible(False)
    plt.suptitle('Y-Value={}'.format(yval), y=0.95, fontsize=10)

def onclick(event):
    yval=event.ydata
    updateChart()
  
plt.gcf().canvas.mpl_connect('button_press_event', onclick)
yval=35000
updateChart()

Alright, so for some reason, the onClick() function simply seems to refuse calling the updateChart() function. When I insert the updateChart() code within the onClick() function, it has no issue. Same thing goes with the colorSelect() function. I find this odd, since the updateChart() function has no problem calling the colorSelect() function. Can anyone explain why this is happening and if there is anyway to call the updateChart() function within the onClick() function.

Thank you for your answers.

Source: Python-3x Questions

LEAVE A COMMENT