plot(matplotlib.pyplot)绘图(条状图)

本文通过读取fandango_scores.csv数据,展示了《复仇者联盟4》在不同媒体上的评分分布,包括用户评价、Metacritic、IMDB和Fandango的平均分数。通过matplotlib绘制了柱状图,直观呈现了评分对比。
摘要由CSDN通过智能技术生成

使用数据地址:链接: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()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值