导入numpy库、pandas库和Matplotlib库
import pandas as pd import numpy import matplotlib.pyplot as plt
导入文件
reviews = pd.read_csv("fandango_score_comparison.csv")
条形图:
生成一个条形图:
cols = ['Metacritic_User', 'IMDB', 'Fandango_Stars', 'Fandango_Ratingvalue', 'RT_norm'] norm_reviews = reviews[cols] num_cols = ['Metacritic_User', 'IMDB', 'Fandango_Stars', 'Fandango_Ratingvalue', 'RT_norm'] #设置柱高 bar_hight = norm_reviews.loc[0,num_cols].values print(bar_hight) #设置条与条的间距 bar_positions = numpy.arange(5) + 1 print(bar_positions) #绘制bar图(条形图) #fig用于控制图,ax用于绘图 fig,ax = plt.subplots() ax.bar(bar_positions, bar_hight, 0.3) plt.show()
图像:
我们可以来设置图像的数据
例如:
#设置bar图 fig,ax = plt.subplots() ax.bar(bar_positions, bar_hight, 0.3) tick_position = range(1, 6) ax.set_xticks(tick_position) ax.set_xticklabels(num_cols, rotation = 45) ax.set_xlabel('Rating Source') ax.set_ylabel('Average Rating') ax.set_title('Average User Rating for Avengers') plt.show()
图像:
还可以制作横着的条形图:
#设置横bar图 fig,ax = plt.subplots() ax.barh(bar_positions, bar_hight, 0.3) tick_position = range(1, 6) ax.set_yticks(tick_position) ax.set_yticklabels(num_cols) ax.set_ylabel('Rating Source') ax.set_xlabel('Average Rating') ax.set_title('Average User Rating for Avengers') plt.show()
图像:
散点图:
生成一个散点图:
cols = ['Metacritic', 'RT_norm'] new_reviews = reviews[cols] fig, ax = plt.subplots() ax.scatter(new_reviews['Metacritic'], new_reviews['RT_norm']) ax.set_xlabel('Metacritic') ax.set_ylabel('RT_norm') plt.show()
图像: