【问题标题】:Python matplotlib - doubling the histogramPython matplotlib - 将直方图加倍
【发布时间】:2021-12-18 13:39:05
【问题描述】:

任务:将 2 个直方图保存在单独的文件中: 收视率直方图和年份直方图。数据取自 rating.list 文件。

问题:第二个直方图输出自己的值,以及第一个直方图的值。

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')

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    尝试在第一次绘图保存后使用plt.close(fig),就在开始第二次绘图之前。

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 2017-06-29
      • 2019-02-13
      • 2014-04-15
      • 2016-10-14
      • 2021-12-20
      • 2011-07-16
      • 2012-01-12
      • 2012-07-12
      相关资源
      最近更新 更多