Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having some trouble getting a top 5 bar chart to open in the below python code: The actual requirement is: Provide a Bar

I am having some trouble getting a top 5 bar chart to open in the below python code:

The actual requirement is: Provide a Bar graph of the top 5 populated States showing their overall population.

import sys import matplotlib.pyplot as plt import matplotlib.image as mpimg

#Empty list states = []

#States.append inserts the items in the list states #State information was retrieved from states.append ( ["Alabama","Montgomery","Camellia",4887680,"flowers/AL.jpg"] ) states.append ( ["Alaska","Juneau"," Ptarmigan",735139,"flowers/AK.jpg"]) states.append ( ["Arizona","Phoenix","Blossom of the Saguaro Cactus",7158020,"flowers/AZ.jpg"]) states.append ( ["Arkansas","Little Rock","Apple blossom",3009730,"flowers/AR.jpg"]) states.append ( ["California","Sacramento","Golden Poppy",39461600,"flowers/CA.jpg"]) states.append ( ["Colorado","Denver","Rocky Mountain Columbine",5691290,"flowers/CO.jpg"]) states.append ( ["Connecticut","Hartford","Mountain Laurel",3571520,"flowers/CT.jpg"]) states.append ( ["Delaware","Dover","Peach blossom",965479,"flowers/DE.jpg"]) states.append ( ["Florida","Tallahassee","Orange blossom",21244300,"flowers/FL.jpg"]) states.append ( ["Georgia","Atlanta","Cherokee Rose",10511100,"flowers/GA.jpg"]) states.append ( ["Hawaii","Honolulu","Yellow Hibiscus/Pua Aloalo",1420590,"flowers/HI.jpg"]) states.append ( ["Idaho","Boise","Syringa",1750540,"flowers/ID.jpg"]) states.append ( ["Illinois","Springfield","Native Violet",12723100,"flowers/IL.jpg"]) states.append ( ["Indiana","Indianapolis","Peony",6695500,"flowers/IN.jpg"]) states.append ( ["Iowa","Des Moines","Wild Rose",3148620,"flowers/IA.jpg"]) states.append ( ["Kansas","Topeka","Native Sunflower",2911360,"flowers/KS.jpg"]) states.append ( ["Kentucky","Frankfort","Goldenrod",4461150,"flowers/KY.jpg"]) states.append ( ["Louisiana","Baton Rouge","Magnolia",4659690,"flowers/LA.jpg"]) states.append ( ["Maine","Augusta","White Pinecone/Tassel",1339060,"flowers/ME.jpg"]) states.append ( ["Maryland","Annapolis","Black-eyed Susan",6035800,"flowers/MD.jpg"]) states.append ( ["Massachusetts","Boston","Mayflower",6882640,"flowers/MA.jpg"]) states.append ( ["Michigan","Lansing","Apple Blossom",9984070,"flowers/MI.jpg"]) states.append ( ["Minnesota","Saint Paul","Pink & White Ladys Slipper",5606250,"flowers/MN.jpg"]) states.append ( ["Mississippi","Jackson","Magnolia",2981020,"flowers/MS.jpg"]) states.append ( ["Missouri","Jefferson City","Hawthorn",6121620,"flowers/MO.jpg"]) states.append ( ["Montana","Helena","Bitterroot",1060660,"flowers/MT.jpg"]) states.append ( ["Nebraska","Lincoln","Goldenrod",1925610,"flowers/NE.jpg"]) states.append ( ["Nevada","Carson City","Sagebrush",3027340,"flowers/NV.jpg"]) states.append ( ["New Hampshire","Concord","Purple Lilac",1353460,"flowers/NH.jpg"]) states.append ( ["New Jersey","Trenton","Purple Violet",8886020,"flowers/NJ.jpg"]) states.append ( ["New Mexico","Santa Fe","Yucca",2092740,"flowers/NM.jpg"]) states.append ( ["New York","Albany","Rose",19530400,"flowers/NY.jpg"]) states.append ( ["North Carolina","Raleigh","Dogwood",10381600,"flowers/NC.jpg"]) states.append ( ["North Dakota","Bismarck","Wild Prairie Rose","758080","flowers/ND.jpg"]) states.append ( ["Ohio","Columbus","Scarlet Carnation",11676300,"flowers/OH.jpg"]) states.append ( ["Oklahoma","Oklahoma City","Mistletoe",3940240,"flowers/OK.jpg"]) states.append ( ["Oregon","Salem","Oregon Grape",4181890,"flowers/OR.jpg"]) states.append ( ["Pennsylvania","Harrisburg","Mountain Laurel",12800900,"flowers/PA.jpg"]) states.append ( ["Rhode Island","Providence","Violet",1058290,"flowers/RI.jpg"]) states.append ( ["South Carolina","Columbia","Yellow Jessamine",5084160,"flowers/SC.jpg"]) states.append ( ["South Dakota","Pierre","Pasqueflower",878698,"flowers/SD.jpg"]) states.append ( ["Tennessee","Nashville","Iris",6771630,"flowers/TN.jpg"]) states.append ( ["Texas","Austin","Bluebonnet",28628700,"flowers/TX.jpg"]) states.append ( ["Utah","Salt Lake City","Sego Lily",3153550,"flowers/UT.jpg"]) states.append ( ["Vermont","Montpelier","Red Clover",624358,"flowers/VT.jpg"]) states.append ( ["Virginia","Richmond","Flowering Dogwood",8501290,"flowers/VA.jpg"]) states.append ( ["Washington","Olympia","Western Rhododendron",7523870,"flowers/WA.jpg"]) states.append ( ["West Virginia","Charleston","Big Rhododendron",1804290,"flowers/WV.jpg"]) states.append ( ["Wisconsin","Madison","Wood Violet",5807410,"flowers/WI.jpg"]) states.append ( ["Wyoming","Cheyenne","Indian Paintbrush",577601,"flowers/WY.jpg"]) states.sort()

def display_states(): """This function will display all US States in alphabetical order""" for i in range (0, len(states)): print ("State: ", states[i][0], " Capitol: ", states[i][1], " Flower: ", states[i][2], " Population: ", states[i][3], " " )

def search_state(): """This function will allow the user to search for a particular state""" state_name = input ("Input the State Name to Search: " ) for i in range (0, 50): if state_name == (states[i][0]): print ("State: ", states[i][0], " Capitol: ", states[i][1], " Flower: ", states[i][2], " Population: ", states[i][3], " " ) img = mpimg.imread((states[i][4])) imgplot = plt.imshow(img) plt.title(states[i][2]) plt.show () if i == 51: print("Please enter a valid state name")

def display_bar_graph_top5(): # convert string array to int, also making list from them for i in range ( 0, len ( states) ): print (max ( numbers )) plt.bar (states[i][0], states[i][3],color ='maroon', width = 0.4) plt.ylabel ( 'State Population' ) plt.xlabel (states[i][0]) plt.show () # Bar graph of the top 5 populated States showing their overall population

def update_population(): """This function will allow the user to update a population in the list states""" state_name = input (' Input the State Name to Search: ') new_population = input (' Enter New State population: ') for i in range (0, 50): if state_name == (states[i][0]): print ( "The following information has been updated in the states list:" ) print (" State: ", states[i][0], " Capitol: ", states[i][1], " Flower: ", states[i][2], " Population: ", new_population, " " ) states[i][3] = (new_population) if i == 51: print("Please enter a valid state name")

while True: print ( "1. Display all U.S. States in Alphabetical order along with the " "Capital, State Population, and Flower " ) print ( "2. Search for a specific state and display the appropriate Capital " "name, State Population, and") print ( "an image of the associated State Flower." ) print ( "3. Provide a Bar graph of the top 5 populated States showing their " "overall population " ) print ( "4. Update the overall state population for a specific state. " ) print ( "5. Exit the program " ) user_input = int ( input ( "Enter the number of your choice: ") ) # add check for int between 1 and 5 else message and return to menu # if user_input ( 0> or 5< ), then if user_input == 1: display_states () elif user_input == 2: search_state () elif user_input == 3: display_bar_graph_top5() elif user_input == 4: update_population () elif user_input == 5: #Exits the program print("Thank you for using the State Information Program!") break else: #Checks for valid user input print("That is not a correct entry, please select 1-5")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago