Python Forum
how to add user input to a dictionary to a graph - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: how to add user input to a dictionary to a graph (/thread-20203.html)



how to add user input to a dictionary to a graph - KINGLEBRON - Jul-31-2019

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


RE: how to add user input to a dictionary to a graph - jefsummers - Jul-31-2019

At first blush, you are having plt.show() inside your loop. You probably want it outside, after the loop to just display it once.


RE: how to add user input to a dictionary to a graph - KINGLEBRON - Jul-31-2019

(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?


RE: how to add user input to a dictionary to a graph - SheeppOSU - Jul-31-2019

(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).