Python Forum
how to add user input to a dictionary to a graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to add user input to a dictionary to a graph
#1
i trying to build a a graph from an api of weather website. i want the user will input 3 cities that i would like to know and then make a dictionry from the api {name of the city: the currently temp from the api} and then make the dictionry to a graph :
(x: name of the city , y: current temperature from the api)

def read_key_from_file():
    hFile = open(file_name,'r')
    for line in hFile:
        Mykey = line
    hFile.close()
    Mykey = line
    return  Mykey

def get_geo_location(location):
    location = Nominatim().geocode(location)
    lat = location.latitude
    lon = location.longitude
    return location, lat, lon


def comperison_graph(Mykey): 

    if daily_temp_dict == {}:
        a = input("Enter first city name in lower case letter: ")
        daily_temp_dict[a] = ""
        b = input("Enter second city name in lower case letter: ")
        daily_temp_dict[b] = ""
        c = input("Enter third city name in lower case letter: ")
        daily_temp_dict[c] = ""
        for city in daily_temp_dict.keys():
                location, lat, lon = get_geo_location(city)
                forecast = forecastio.load_forecast(Mykey, lat, lon)
                daily_temp_dict[city] = forecast.currently().temperature
                data =  daily_temp_dict
                names = list(data.keys())
                print(names)
                values = list(data.values())
                print(values)
                plt.bar(0,values[0],tick_label=names[0])
                plt.bar(1,values[1],tick_label=names[1])
                plt.bar(2,values[2],tick_label=names[2])
                plt.xticks(range(0,3),names)
                plt.savefig('fruit.png')
                plt.show()
the problem is that im getting in a result a graph like this :


[Image: raVSVxH]
https://imgur.com/raVSVxH

and i need only the last one
Reply
#2
At first blush, you are having plt.show() inside your loop. You probably want it outside, after the loop to just display it once.
Reply
#3
(Jul-31-2019, 08:18 PM)jefsummers Wrote: At first blush, you are having plt.show() inside your loop. You probably want it outside, after the loop to just display it once.

how it should be?
Reply
#4
(Jul-31-2019, 08:25 PM)KINGLEBRON Wrote: how it should be?
plt.show() in inside the for loop. So every time the loop plays, it will show the graph, if you want only the last result, then move it outside of the for loop (indent it backwards once).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,280 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,361 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,962 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,298 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,799 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,132 Nov-04-2019, 02:02 PM
Last Post: perfringo
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,491 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,556 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 10,727 Mar-02-2019, 10:11 PM
Last Post: johneven
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,177 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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