本篇主要内容来自于唐宇迪-机器学习课程的数据可视化章节,此文只做个人实操和理解用。
条形图
此次实战的数据为美国各大电影网站对各大电影的评分。
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt #数据分析时先导入这三库总没错
scores = pd.read_csv(r'/Users/herenyi/Desktop/CORE/matpltlib/fandango_scores.csv')
cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'] #画图需要的列
new_score = scores[cols] #选取到了我们所需的列并建立了新的Dataframe数据框
new_score_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'] # 要画条形的柱列表
bar_heights = new_score.ix[0, new_score_cols ].values #首先是柱高度参数,此次我们选取的是0行电影的评分,也就是Avengers: Age of Ultron这部电影。
bar_positions = np.arange(5) + 0.75 #然后是柱与0轴的位置,可精确调整 。
fig, ax = plt.subplots()
ax.bar(bar_positions, bar_heights, 0.5) #画出柱形图, 最后的0.5表示柱的宽度。如果想调节更多参数,参考help(ax.bar)文档。
接下来我们对图表进行美化。
ax.set_xticks(range(1,6) #刻度
ax.set_xticklabels(new_score_cols, rotation=30) #刻度标签
ax.set_xlabel('Rating Source') #x轴标题
ax.set_ylabel('Average Rating') #y轴标题
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)') #图表标题
我们也可以作水平条形图,这个跟刚刚的条形图没啥区别,只是作图时,一个是ax.bar,水平条形图则是ax.barh,然后修改下细节。具体做法如下:
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt #数据分析时先导入这三库总没错
scores = pd.read_csv(r'/Users/herenyi/Desktop/CORE/matpltlib/fandango_scores.csv')
cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'] #画图需要的列
new_score = scores[cols] #选取到了我们所需的列并建立了新的Dataframe数据框
new_score_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'] # 要画条形的柱列表
bar_widths = new_score.ix[0, new_score_cols ].values # **这个是柱宽度,要跟条形图区分。**
bar_positions = np.arange(5) + 0.75 #然后是柱与0轴的位置,可精确调整 。
fig, ax = plt.subplots()
ax.barh(bar_positions, bar_widths, 0.5) #画出柱形图, 如果想调节更多参数,参考help(ax.bar)文档。
ax.set_yticks(range(1,6)) #刻度
ax.set_yticklabels(new_score_cols) #刻度值
ax.set_xlabel('Average Rating')
ax.set_ylabel('Rating Source') #x轴标题和y轴标题互换
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)') #图表标题
散点图
接下来让我们看看散点图,散点图能够很好的帮我们把数据点呈现出来,让我们能从中发现其中隐藏着的关系。
fig, ax = plt.subplots()
ax.scatter (new_score['Fandango_Ratingvalue'], new_score['RT_user_norm']) #传入x,y点
ax.set_xlabel('Fandango')
ax.set_ylabel('Rotten Tomatoes')
原辅导视频中还做了转置x,y轴,我们也试着做一下。
fig = plt.figure(figsize = (5,10))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.scatter(new_score['Fandango_Ratingvalue'], new_score['RT_user_norm'])
ax1.set_xlabel('Fandango')
ax1.set_ylabel('Rotten Tomatoes')
ax2.scatter(new_score['RT_user_norm'], new_score['Fandango_Ratingvalue'])
ax2.set_xlabel('Rotten Tomatoes')
ax2.set_ylabel('Fandango')
柱形图
接下来是柱形图和盒形图的实操了,可视化其实没什么技术难度,关键是把绘图的各种函数和参数就清楚就好,接下来的就是照葫芦画瓢,可视化虽然是结果的最终呈现,但是难的都在前面整理的工作。
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
scores = pd.read_csv(r'/Users/herenyi/Desktop/CORE/matpltlib/fandango_scores.csv') #没啥好说的,导库读取文件。
cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
new_score = scores[cols]
fandango_distribution = new_score['Fandango_Ratingvalue'].value_counts() #为了了解fandango评分分布,筛选出该列并用value_counts()统计各评分频数。
fandango_distribution = fandango_distribution.sort_index() #评分从小到大排列
imdb_distribution = new_score['IMDB_norm'].value_counts()
imdb_distribution = imdb_distribution.sort_index() #同理可得imdb分布,此时我们明白了数据频数分布,接下来就是作图了。
接下来开始做柱状图,
fig, ax = plt.subplots()
ax.hist(new_score['Fandango_Ratingvalue'], bins = 20,)#hist 为柱状图函数,bins代表柱的数量,如果不指定系统会帮你分一定数量的组,
原案例中还做了四个网站各自评分的柱状图,我们试着还原下。
fig = plt.figure(figsize=(5,20))
ax1 = fig.add_subplot(4,1,1)
ax2 = fig.add_subplot(4,1,2)
ax3 = fig.add_subplot(4,1,3)
ax4 = fig.add_subplot(4,1,4)
ax1.hist(new_score['Fandango_Ratingvalue'], bins=20, range=(0, 5))
ax1.set_title('Distribution of Fandango Ratings')
ax1.set_ylim(0, 50)
ax2.hist(new_score['RT_user_norm'], 20, range=(0, 5))
ax2.set_title('Distribution of Rotten Tomatoes Ratings')
ax2.set_ylim(0, 50)
ax3.hist(new_score['Metacritic_user_nom'], 20, range=(0, 5))
ax3.set_title('Distribution of Metacritic Ratings')
ax3.set_ylim(0, 50)
ax4.hist(new_score['IMDB_norm'], 20, range=(0, 5))
ax4.set_title('Distribution of IMDB Ratings')
ax4.set_ylim(0, 50)
箱体图
最后就是箱体图了。
#选定哪几列去做箱体图
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue']
fig, ax = plt.subplots()
ax.boxplot(new_score[num_cols].values) #制作箱体图
ax.set_xticklabels(num_cols, rotation=90)
ax.set_ylim(0,5)