Python Forum
Python Assignment 3 - Applied Data Science - 2.3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Assignment 3 - Applied Data Science - 2.3
#1
Hey all,

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?
Reply
#2
(Jun-03-2020, 05:42 PM)eyavuz21 Wrote: Hey all,

Here is an improved version of the code above:

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()
limit = False 
def onclick(event):
    plt.cla()
    plt.bar(labels,scores,align='center', alpha=0.5,yerr=add)
    plt.xticks(labels)
    global limit 
    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?
Reply
#3
Hey all,

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
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
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,df.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? Am I on the right lines?
Reply
#4
List index out of range error occurs in Python when we try to access an undefined element from the list. The only way to avoid this error is to mention the indexes of list elements properly. ... And we know that the index of a list starts from 0 that's why in the list, the last index is 2, not 3
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
(Jun-05-2020, 10:37 AM)pyzyx3qwerty Wrote: List index out of range error occurs in Python when we try to access an undefined element from the list. The only way to avoid this error is to mention the indexes of list elements properly. ... And we know that the index of a list starts from 0 that's why in the list, the last index is 2, not 3

Hey, I think I am properly indexing, but I'm not sure why the code is giving me this error: there is only one value in the list 'limits', and so limits[0] should give me that. Surely?
Reply
#6
Please show the full error/traceback, with proper tags added
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#7
(Jun-05-2020, 04:21 PM)pyzyx3qwerty Wrote: Please show the full error/traceback, with proper tags added

Hey,

Here is the full error message:

Traceback (most recent call last):
  File "<input>", line 45, in <module>
IndexError: list index out of range
Is my code overall on the right lines to getting the output I want?
Reply
#8
Also, I'm confused. You have posted three codes - which one is the code in which you are getting this error?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#9
(Jun-06-2020, 04:52 AM)pyzyx3qwerty Wrote: Also, I'm confused. You have posted three codes - which one is the code in which you are getting this error?

The most recent one!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python for Everybody 5.2 assignment baba04201 20 173,455 Jul-25-2023, 04:15 PM
Last Post: MicaelSchutz
  AI Techniques applied to a Game insignia47 0 957 Jun-10-2022, 07:33 PM
Last Post: insignia47
  Python for Everybody 3.1 assignment ramadan2099 18 45,471 Jan-23-2021, 06:27 AM
Last Post: KonohaHokage
  Coursera python for everybody 5.2 assignment SteppentigerV2 11 12,900 Oct-22-2020, 11:57 AM
Last Post: Larz60+
  [split] Python for Everybody 5.2 assignment ramadan2099 3 12,078 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  Applied Data Science with Python - homework 2.2 (Weather plotting) eyavuz21 4 3,385 Jun-03-2020, 07:09 PM
Last Post: eyavuz21
  Python Password Saver Assignment sshellzr21 2 6,200 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Python for Everybody 3.3 assignment ramadan2099 7 31,774 Apr-08-2020, 06:49 AM
Last Post: DeaD_EyE
  Python for everyone course assignment 5.2 ofekx 3 8,557 Dec-23-2019, 08:41 PM
Last Post: nilamo
  [split] Python for Everybody 5.2 assignment jonchanzw 4 8,490 Oct-22-2019, 08:08 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020