matplotlib简单操作(三)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/8/11 12:12
# @Author  : limingyu
# @Site    : 
# @File    : Test_Matplotlib_fandango.py
# @Software: PyCharm
#数据是:电影评分

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

reviews = pd.read_csv("fandango_score_comparison.csv")  #读数据
#各评分媒体集合
cols = ['RT_user_norm','Metacritic_user_nom','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']
norm_reviews = reviews[cols]  #取上述几列内容
print(norm_reviews[:1]) #打印第0行数据
#   RT_user_norm Metacritic_user_nom  IMDB_norm ... Fandango_Stars
# 0      4.3             3.55           3.9     ...      5.0


#--------------------------------柱状图bar------------------------------#
# 柱形图高度(媒体的评分值),各柱离坐标轴距离(各媒体间的距离)
bar_heights = norm_reviews.ix[0,cols].values  #取0行所有列的值
print(bar_heights)  #得到评分即柱高[4.3  3.55 3.9  4.5  5.]
bar_positions = np.arange(5) + 0.75 #每个柱到原点距离
print(bar_positions)  #[0.75 1.75 2.75 3.75 4.75]
#fig, ax = plt.subplots(2,2),其中参数分别代表子图的行数和列数,一共有 2x2 个图像。
#函数返回一个figure图像和一个子图ax的array列表。ax用于画图,figure用于控制图
fig,ax = plt.subplots()
print(fig)  #Figure(640x480)
print(ax)  #子图AxesSubplot(0.125,0.11;0.775x0.77)
#0.3表示柱的宽度
ax.bar(bar_positions,bar_heights,0.3)
plt.show()


#柱形图高度(媒体的评分值),各柱离坐标轴距离(各媒体间的距离)
cols = ['RT_user_norm','Metacritic_user_nom','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']
bar_heights = norm_reviews.ix[0,cols].values  #取0行所有列的值
bar_positions = np.arange(5) + 0.75 #每个柱到原点距离
tick_positions = range(1,6)
fig,ax = plt.subplots()
#柱形图横着画
ax.bar(bar_positions,bar_heights,0.5)
#柱形图竖着画
#ax.barh(bar_positions,bar_heights,0.5)
ax.set_xticks(tick_positions)
ax.set_xticklabels(cols,rotation=45)

ax.set_xlabel('Rating Source')
ax.set_ylabel('Average Rating')
ax.set_title('Average User Rating For Avengers :Age of Ultron (2015)')
plt.show()


#--------------------------------散点图scatter------------------------------#
#画两个媒体评分分布规则
fig,ax = plt.subplots()
ax.scatter(norm_reviews['Fandango_Ratingvalue'],norm_reviews['RT_user_norm'])
ax.set_xlabel('Fandango')
ax.set_ylabel('Rotten Tomatoes')
plt.show()


##画多个媒体评分分布规则
fig = plt.figure(figsize=(5,10))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.scatter(norm_reviews['Fandango_Ratingvalue'],norm_reviews['RT_user_norm'])
ax1.set_xlabel('Fandango')
ax1.set_ylabel('Rotten Tomatoes')
ax2.scatter(norm_reviews['RT_user_norm'],norm_reviews['Fandango_Ratingvalue'])
ax2.set_xlabel('Rotten Tomatoes')
ax2.set_ylabel('Fandango')
plt.show()

 

猜你喜欢

转载自blog.csdn.net/mingyuli/article/details/81586312