使用数据地址:链接:https://pan.baidu.com/s/1wXtZRDcM-JKk_dIDRyd_dg?pwd=pyth
提取码:pyth
提取一些数据,用来绘图用
import pandas as pd
reviews = pd.read_csv('fandango_scores.csv')
cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']#取一些列做新的数据,file列是电影名,后面的都是媒体名,媒体给电影评分。
norm_reviews = reviews[cols]#取一些列做新的数据norm_reviews
print(norm_reviews[0:2])
'''
FILM RT_user_norm Metacritic_user_nom \
0 Avengers: Age of Ultron (2015) 4.3 3.55
1 Cinderella (2015) 4.0 3.75
IMDB_norm Fandango_Ratingvalue Fandango_Stars
0 3.90 4.5 5.0
1 3.55 4.5 5.0
'''
#每个媒体的评分都绘制成一个柱子
import matplotlib.pyplot as plt
from numpy import arange
#The Axes.bar() method has 2 required parameters, left and height.
#We use the left parameter to specify the x coordinates of the left sides of the bar.
#We use the height parameter to specify the height of each bar
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
print(norm_reviews.loc[0, num_cols])#取第0行的数据来绘图
'''
RT_user_norm 4.3
Metacritic_user_nom 3.55
IMDB_norm 3.9
Fandango_Ratingvalue 4.5
Fandango_Stars 5.0
Name: 0, dtype: object
'''
bar_heights = norm_reviews.loc[0, num_cols].values#取第0行,num_cols中列的数值,此数据用来作为条状图的条高
print (bar_heights)#[4.3 3.55 3.9 4.5 5.0]
bar_positions = arange(5) + 0.75#表示的是每个柱子的中间位置距离左边原点的距离
bar_positions = arange(5) + 0.75#表示的是每个柱子的中间位置距离左边原点的距离
print(bar_positions)#[0.75 1.75 2.75 3.75 4.75]
fig, ax = plt.subplots()#subplot函数返回一个figure,一个ax
ax.bar(bar_positions, bar_heights, 0.5)#0.5表示的是柱子的粗细
plt.show()
这种图像过于简洁,增加x、y轴描述,图像标题等,需对ax对象进行操作,每个柱子(条)均可定义名称,需对ax对象使用set_xticklabels函数。
#By default, matplotlib sets the x-axis tick labels to the integer values the bars
#spanned on the x-axis (from 0 to 6). We only need tick labels on the x-axis where the bars are positioned.
#We can use Axes.set_xticks() to change the positions of the ticks to [1, 2, 3, 4, 5]:
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews.loc[0, num_cols].values#取得第0行,num_cols中的数据用来绘图
tick_positions = range(1,6)#1, 2, 3, 4, 5
fig, ax = plt.subplots()
ax.bar(bar_positions, bar_heights, 0.5)#传入条状图参数,条位置、条高、条的粗细
ax.set_xticks(tick_positions)#传入参数tick_positions作为每个条的位置1, 2, 3, 4, 5
ax.set_xticklabels(num_cols, rotation=45)#使用set_xticklabels函数对每个条定义名称,num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'],rotation为旋转角度。
ax.set_xlabel('Rating Source')
ax.set_ylabel('Average Rating')
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
plt.show()
以上柱形图均为竖立型柱形图,想要画出横着的柱形图,需使用函数barh
import matplotlib.pyplot as plt
from numpy import arange
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_widths = norm_reviews.loc[0, num_cols].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)
fig, ax = plt.subplots()
ax.barh(bar_positions, bar_widths, 0.5)#横着画柱形图,需使用barh函数,而非bar函数
ax.set_yticks(tick_positions)
ax.set_yticklabels(num_cols)
ax.set_ylabel('Rating Source')
ax.set_xlabel('Average Rating')
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
plt.show()