Jun-03-2020, 05:42 PM
Hey all,
I have the following code below:
However, this does not seem to work.
Would anybody be able to give me a helping hand?
I have the following code below:
import pandas as pd import numpy as np from scipy import stats import matplotlib.pyplot as plt np.random.seed(12345) scores = [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()] 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 labels = [1992,1993,1994,1995] add = [add1992,add1992,add1994,add1995] plt.bar(labels,scores,align='center', alpha=0.5,yerr=add) plt.xticks(labels) plt.show() 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() def onclick(event): plt.cla() plt.bar(labels,scores,align='center', alpha=0.5,yerr=add) plt.xticks(labels) limit = event.ydata print(limit) plt.gcf().canvas.mpl_connect('button_press_event', onclick) dict = {mean1992:add1992,mean1993:add1993,mean1994:add1994,mean1995:add1995} colours = [] for key,value in dict.items(): if limit > (key+(value)): colour = 1 colours.append(colour) if len(colours) > 4: plt.gcf().canvas.mpl_disconnect(limit) elif limit < (key-(value)): colour = 0 colours.append(colour) if len(colours) > 4: plt.gcf().canvas.mpl_disconnect(limit) elif (limit < (key+(value))) & (limit > (key-(value))): colour = ((key+(value))-limit)/((key+value)-(key-value)) colours.append(colour) if len(colours) > 4: plt.gcf().canvas.mpl_disconnect(limit)Ideally, I want the user to be able to click on the graph and for a Y-value to be selected, which is represented by the variable 'limit' in the code above. This seems to work, however, I would like to use the value assigned to this variable for the remainder of the code.
However, this does not seem to work.
Would anybody be able to give me a helping hand?