Task: save 2 histograms in separate files: a histogram of ratings and a histogram of years. The data is taken from the ratings.list file.
Problem: The second histogram outputs its own values, and the values of the first histogram.
import matplotlib.pyplot as plt
try:
with open('ratings.list', 'r') as reader:
line = reader.readline()
while line == '\n' or 'New' not in line and 'Distribution' not in line:
line = reader.readline()
line = reader.readline()
ratings = []
years = []
while line != '' and line != '\n' and line != 'BOTTOM 10 MOVIES (1500 VOTES)':
columns = line.split()
initial_tabs = 13
index = len(columns[0]) len(columns[1]) len(columns[2]) initial_tabs
title = line[index:len(line)]
with open('top250_movies.txt', 'a') as writer:
line = title '\n'
writer.write(line)
ratings.append(columns[2])
year = line[(line.index('(') 1):line.index(')')]
years.append(year)
years.sort()
line = reader.readline()
rate_hist = plt.hist(ratings, bins=10)
plt.title('Ratings')
plt.xlabel('Ratings')
plt.ylabel('Movies count')
ys = rate_hist[0]
xs = rate_hist[1]
plt.savefig('ratings.png')
year_hist = plt.hist(years, bins=50)
plt.title('Years')
plt.xlabel('Years')
plt.xticks(rotation=90, fontsize=4)
plt.ylabel('Movies count')
ys = year_hist[0]
xs = year_hist[1]
plt.savefig('years.png')
except IOError:
print('File does not exist')
CodePudding user response:
Try to use plt.close(fig)
after the first plot save, just before you start the second one.