Home > front end >  How to select all keys in loop for x
How to select all keys in loop for x

Time:05-12

part of the Gasprices.txt is like

04-05-1993:1.068

04-12-1993:1.079

04-19-1993:1.079

05-09-1994:1.045

05-16-1994:1.046

05-23-1994:1.05


import matplotlib.pyplot as plt

import numpy as np


with open('c:/Gasprices.txt', 'r') as file:
    td = dict()
    for line in file:
        year = line[6:10]
        price = float(line[11:])
        td.setdefault(year, []).append(price)
    for k, v in td.items():
        Year =f'{k}'
        avg_price = f'{sum(v)/ len(v)}'
        print(Year, avg_price)

The result for the upper code is

1993 1.0711538461538466

1994 1.0778653846153845

1995 1.1577115384615386

1996 1.2445283018867925

1997 1.2442499999999999

1998 1.071711538461538

1999 1.1760576923076924

2000 1.522730769230769

2001 1.4603018867924529

2002 1.385961538461538

2003 1.603019230769231

2004 1.8946923076923083

2005 2.314461538461538

2006 2.6182692307692315

2007 2.8434716981132078

2008 3.2989038461538462

2009 2.4058269230769236

2010 2.835057692307693

2011 3.576423076923077

2012 3.6796415094339627

2013 3.651441176470588


and i want to use this result for drawing graph using matplotlib. But because of the loop, if i use code like this

import matplotlib.pyplot as plt

import numpy as np


with open('c:/Gasprices.txt', 'r') as file:
    td = dict()
    for line in file:
        year = line[6:10]
        price = float(line[11:])
        td.setdefault(year, []).append(price)
    for k, v in td.items():
        Year =f'{k}'
        avg_price = f'{sum(v)/ len(v)}'
        print(Year, avg_price)


x=Year
y=avg_price
plt.plot(x,y, 'o--')
plt.title('Average gas price per year in US')
plt.xlabel('year')
plt.ylabel('Avg.gas price per gallon[$]')
plt.grid()
plt.xticks(np.arange(1993, 2014, 1))
plt.xticks(rotation=45)
plt.yticks(np.arange(1.0, 4.0, 0.5))
plt.tight_layout()

plt.show()

only the last information 2013 3.651441176470588 is drawn on the graph.

How can i put all of the year information and avg_price information respectively in x and y?

CodePudding user response:

You need to add those information to lists (here xand y) :

x = []
y = []
with open('c:/Gasprices.txt', 'r') as file:
    td = dict()
    for line in file:
        year = line[6:10]
        price = float(line[11:])
        td.setdefault(year, []).append(price)
    for k, v in td.items():
        Year = f'{k}'
        avg_price = f'{sum(v)/ len(v)}'
        print(Year, avg_price)
        x.append(Year)
        y.append(avg_price)

Since your data are strings, you need to cast them :

x = [int(i) for i in x]  # Years are int
y = [float(i) for i in y]  # Prices are float

Then you can call your plot the same way :

plt.plot(x,y, 'o--')
plt.title('Average gas price per year in US')
plt.xlabel('year')
plt.ylabel('Avg.gas price per gallon[$]')
plt.grid()
plt.xticks(np.arange(1993, 2014, 1))
plt.xticks(rotation=45)
plt.yticks(np.arange(1.0, 4.0, 0.5))
plt.tight_layout()

plt.show()
  • Related