机器学习初入门03 - Matplotlib
这一部分很简单,所以以代码的形式给出,在实际学习开发中,Matplotlib最好只把它当成一个画图的工具来用,没有必要深究其实现原理是什么。
一、折线图的绘制
import pandas as pd
unrate = pd.read_csv("unrate.csv")
print(unrate.head(5))
#pandas应该会自动把xxxx/xx/xx转化为标准时间格式xxxx-xx-xx,如果没有,用下面一行代码实现
# unrate["DATE"] = pd.to_datetime(unrate["DATE"])
原始数据: 代码执行结果:
import matplotlib.pyplot as plt
#画图
plt.plot()
#把画的图显示出来
plt.show()
first_twelve = unrate[0:12]
#第一个参数为横轴,第二个参数为纵轴
plt.plot(first_twelve["DATE"], first_twelve["VALUE"])
plt.show()
#可以看到上图的横坐标丑的一匹,怎么办呢
plt.plot(first_twelve["DATE"], first_twelve["VALUE"])
#指定x轴标签的角度
plt.xticks(rotation=45)
plt.show()
#给图像加标签
plt.plot(first_twelve["DATE"], first_twelve["VALUE"])
plt.xticks(rotation=45)
plt.xlabel("DATA")
plt.ylabel('Unemployment Rate')
plt.title('Monthly unemployment Trends, 1948')
plt.show()
二、子图操作
import matplotlib.pyplot as plt
#定义画图区域
fig = plt.figure()
#画子图
#前两个参数代表是一个2*2的画图区域,最后一个参数表示该子图的位置
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,4)
plt.show()
import numpy as np
#指定画图区域的大小
fig = plt.figure(figsize = (3,3))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(np.random.randint(1,5,5), np.arange(5))
ax2.plot(np.arange(10)*3, np.arange(10))
plt.show()
import pandas as pd
unrate = pd.read_csv("unrate.csv")
unrate["DATE"] = pd.to_datetime(unrate["DATE"])
unrate['MONTH'] = unrate['DATE'].dt.month
fig = plt.figure(figsize=(6,3))
#在一个图里画多条折线,c指定颜色,可以直接是颜色名称,也可以是RGB值
plt.plot(unrate[0:12]["MONTH"], unrate[0:12]['VALUE'], c='red')
plt.plot(unrate[12:24]["MONTH"], unrate[12:24]['VALUE'], c='blue')
plt.show()
fig = plt.figure(figsize=(10, 6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
subset = unrate[start_index:end_index]
label = str(1948 + i)
plt.plot(subset['MONTH'], subset["VALUE"], c=colors[i], label=label)
#标签框出现在哪
plt.legend(loc='best')
plt.show()
三、条形图与散点图
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'
]
norm_reviews = reviews[cols]
print(norm_reviews[:1] , '\n')
import matplotlib.pyplot as plt
from numpy import arange
num_cols = [
'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue',
'Fandango_Stars'
]
#定义条形图的条高
框也一样。
框是指定column,但是在python3中ix是不赞成使用的
# bar_heights = norm_reviews.ix[0,num_cols].values
bar_heights = norm_reviews.loc[0][num_cols].values
print(norm_reviews.loc[0][num_cols], '\n\n', bar_heights, '\n')
#定义条的位置,即离原点有多远
bar_positions = arange(5) + 0.75
print(bar_positions)
ax = plt.subplot()
#画条形图,第三个参数定义条宽
ax.bar(bar_positions, bar_heights, 0.3)
plt.show()
bar_heights = norm_reviews.loc[0][num_cols].values
#python range() 函数可创建一个整数列表
tick_positions = range(1, 6)
ax = plt.subplot()
ax.bar(bar_positions, bar_heights, 0.5)
ax.set_xticks(tick_positions)
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: Age of Ultron (2015)')
#如果图片显示不全,用下面的命令
plt.tight_layout()
plt.show()
bar_widths = norm_reviews.loc[0][num_cols].values
tick_positions = range(1, 6)
ax = plt.subplot()
ax.barh(bar_positions, bar_widths, 0.5)
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.tight_layout()
plt.show()
#画散点图
ax = plt.subplot()
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()
四、柱形图与盒图
import pandas as pd
import matplotlib.pyplot as plt
reviews = pd.read_csv('fandango_scores.csv')
cols = [
'FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm',
'Fandango_Ratingvalue', 'Fandango_Stars'
]
norm_reviews = reviews[cols]
# print(norm_reviews[:5])
fandango_distribution = norm_reviews['Fandango_Ratingvalue'].value_counts()
fandango_distribution = fandango_distribution.sort_index()
imdb_distribution = norm_reviews['IMDB_norm'].value_counts()
imdb_distribution = imdb_distribution.sort_index()
ax = plt.subplot()
个
ax.hist(norm_reviews['Fandango_Ratingvalue'])
# ax.hist(norm_reviews['Fandango_Ratingvalue'],bins=20)
# range指定显示在图上的总区间
# ax.hist(norm_reviews['Fandango_Ratingvalue'], range=(4, 5), bins=20)
plt.show()
fig = plt.figure(figsize=(5, 20))
ax1 = fig.add_subplot(1, 4, 1)
ax2 = fig.add_subplot(1, 4, 2)
ax3 = fig.add_subplot(1, 4, 3)
ax4 = fig.add_subplot(1, 4, 4)
ax1.hist(norm_reviews['Fandango_Ratingvalue'], bins=20, range=(0, 5))
ax1.set_title('Distribution of Fandango Ratings')
#指定y轴区间
ax1.set_ylim(0, 50)
ax2.hist(norm_reviews['RT_user_norm'], 20, range=(0, 5))
ax2.set_title('Distribution of Rotten Tomatoes Ratings')
ax2.set_ylim(0, 50)
ax3.hist(norm_reviews['Metacritic_user_nom'], 20, range=(0, 5))
ax3.set_title('Distribution of Metacritic Ratings')
ax3.set_ylim(0, 50)
ax4.hist(norm_reviews['IMDB_norm'], 20, range=(0, 5))
ax4.set_title('Distribution of IMDB Ratings')
ax4.set_ylim(0, 50)
plt.show()
#画盒图
ax = plt.subplot()
ax.boxplot(norm_reviews['RT_user_norm'])
ax.set_xticklabels(['Rotten Tomatoes'])
ax.set_ylim(0, 5)
plt.show()
num_cols = [
'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue'
]
fig, ax = plt.subplots()
ax.boxplot(norm_reviews[num_cols].values)
ax.set_xticklabels(num_cols, rotation=90)
ax.set_ylim(0, 5)
plt.tight_layout()
plt.show()
机器学习初入门03 - Matplotlib的更多相关文章
- 机器学习初入门04 – Seaborn(持续更新)
Seaborn库可以说是在matplotlib库上的一个封装,它给我们提供了非常丰富的模板 一.整体布局风格设置 import seaborn as sns import numpy as np im ...
- 机器学习初入门02 - Pandas的基本操作
之前的numpy可以说是一个针对矩阵运算的库,这个Pandas可以说是一个实现数据处理的库,Pandas底层的许多函数正是基于numpy实现的 一.Pandas数据读取 1.pandas.read_c ...
- 机器学习初入门01-numpy的基础用法
一.numpy基础结构 1. numpy.genformtxt('路径名', delimiter = '分割符', dytype = 读取方式如str ):读取一个文件,返回一个numpy.ndarr ...
- 机器学习简易入门(四)- logistic回归
摘要:使用logistic回归来预测某个人的入学申请是否会被接受 声明:(本文的内容非原创,但经过本人翻译和总结而来,转载请注明出处) 本文内容来源:https://www.dataquest.io/ ...
- CSS3基础入门03
CSS3 基础入门03 线性渐变 在css3当中,通过渐变属性实现之前只能通过图片实现的渐变效果.渐变分为线性渐变和径向渐变以及重复渐变三种.线性渐变的模式主要是颜色从一个方向过渡到另外一个方向,而径 ...
- 【网络爬虫入门03】爬虫解析利器beautifulSoup模块的基本应用
[网络爬虫入门03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.B ...
- SpringBoot 初入门
SpringBoot 初入门 关于介绍什么之类的就不讲了,主要做一下学习记录. 1. 启动方式 IDEA 启动 命令行启动: mvn spring-boot:run 部署到服务器启动: 先进行打包, ...
- Andrew Ng机器学习算法入门(一):简介
简介 最近在参加一个利用机器学习来解决安全问题的算法比赛,但是对机器学习的算法一直不了解,所以先了解一下机器学习相关的算法. Andrew Ng就是前段时间从百度离职的吴恩达.关于吴恩达是谁,相信程序 ...
- 机器学习入门03 - 降低损失 (Reducing Loss)
原文链接:https://developers.google.com/machine-learning/crash-course/reducing-loss/ 为了训练模型,需要一种可降低模型损失的好 ...
随机推荐
- C# 颜色对照表
参考资料 :https://www.cnblogs.com/msgarden/p/4949272.html Color.AliceBlue 240,248,255 Color.LightSalmon ...
- if 和 elif 的区别
if: 如果一个判断中用if, 程序他会遍历所有的if, 即使你的判断条件遍历到了, 也会继续执行, 直到遍历完所有的if. elif: 而elif呢, 则效率很高. 只要遍历到你的判断条件, ...
- FZU Monthly-201901 获奖名单
FZU Monthly-201901 获奖名单 冠军: S031702338 郑学贵 一等奖: S031702524 罗继鸿 S031702647 黄海东 二等奖: S031702413 韩洪威 S0 ...
- pycharm 取消空格,逗号 等符号的自动补全
- 浅谈SDN架构下的运维工作
导读 目前国内的网络运维还处于初级阶段,工作人员每天就像救火一样,天天疲于奔命.运维人员只能埋头查找系统运行的日志,耗时耗力,老眼昏花不说,有时候忙了半天还一无所获,作为运维工程师的你,有木有遇到过类 ...
- 多Tomcat多JDK版本的Window服务添加配置方式
本文所讲的配置方式只适合Windows系统,所牵扯的软件2个解压的jdk,2个解压的tomcat. 1.环境初始化 将两个jdk和tomcat解压到我们指定的目录下.修改两个tomcat的端口:必须修 ...
- jstl 使用
1.需要导入: 1)jstl.jar 2)standard.jar 引入:jsp相应的核心库:<%@taglib prefix="c" uri="http:// ...
- Android An unexpected exception occurred while creating a change object. see the error log for more details
今天再给Android项目工程中的包重命名时出现了这个错误(之前重命名的时候就没有出现,郁闷):An unexpected exception occurred while creating a ch ...
- Can't create component 'xxx.xxx.xxx' as it has dependencies to be satisfied
问题描述: Can't create component 'xxx.xxx.xxx' as it has dependencies to be satisfied. 问题原由: 没有对新建的实体映射类 ...
- Fiddler抓包调试前端脚本代码
0.写在前面的话 之前看了阮一峰老师关于互联网协议入门的博客,受益匪浅,接着再去体会了下HTTP协议,就想着看实际网络访问中的那些HTTP请求头和响应是什么样的.Chrome的调试工具的Network ...