机器学习入门(一)之Matplotlib库

一、折线图绘制

代码:plt_1.py
文件:unratre.csv
简介:matplotlib库的一些相关操作

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#表格当年的失业率,作折线图
import pandas as pd
import matplotlib.pyplot as plt
unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])#类型转换,datetime转换为
print(unrate.head(12))
plt.plot()
plt.show()

first_twelve = unrate[0:12]#取出前十个样本
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])#左边x轴,右边y轴
plt.show()

#While the y-axis looks fine, the x-axis tick labels are too close together and are unreadable
#We can rotate the x-axis tick labels by 90 degrees so they don't overlap
#We can specify degrees of rotation using a float or integer value.
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45)#左边变换 rotation角度
#print help(plt.xticks)
plt.show()

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=90)
plt.xlabel('Month')#x轴
plt.ylabel('Unemployment Rate')#y轴
plt.title('Monthly Unemployment Trends, 1948')
plt.show()
  • unrate[‘DATE’] = pd.to_datetime(unrate[‘DATE’]);强制的类型转换成datetime,这个要稍微记一下。
  • plt.plot(参数1,参数2) 绘制折线图,参数1是x轴内容,参数2是y轴内容
  • plt.show() 显示折线图
  • plt.xticks(rotation=45) rotation是角度,可以自行设置
  • plt.xlabel(‘Month’)#x轴
  • plt.ylabel(‘Unemployment Rate’)#y轴

二、子图操作

代码:plt_2.py
文件:unratre.csv
简介:matplotlib库的一些相关操作

  • fig.add_suplot(参数1,参数2,参数3) 多个子图的操作,参数1参数2结合就是矩阵

子图的示意

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#add_subplot(first,second,index) first means number of Row,second means number of Column.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
first_twelve = unrate[0:12]
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=90)
plt.xlabel('Month')
plt.ylabel('Unemployment Rate')
plt.title('Monthly Unemployment Trends, 1948')
plt.show()

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)# 2*2的区间上四个子图,1表示第一个地方,
ax2 = fig.add_subplot(2,2,2)#2表示第二个地方
ax2 = fig.add_subplot(2,2,4)#4表示第四个地方
plt.show()


fig = plt.figure()
#fig = plt.figure(figsize=(3, 3)),figsize的意思是画图区域的大小
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))
#np.random.randint(参数1,参数2,参数3)
ax2.plot(np.arange(10)*3, np.arange(10))
#ax2是第二个子图
plt.show()

unrate['MONTH'] = unrate['DATE'].dt.month
unrate['MONTH'] = unrate['DATE'].dt.month
fig = plt.figure(figsize=(6,3))

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):
    start_index = i*12
    end_index = (i+1)*12
    subset = unrate[start_index:end_index]
    label = str(1948 + i)
    plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)#label是标签名
plt.legend(loc='best')#显示右上角的名字,best放在哪里合适自动设置
#print help(plt.legend)
plt.show()

fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
    start_index = i*12
    end_index = (i+1)*12
    subset = unrate[start_index:end_index]
    label = str(1948 + i)
    plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)
plt.legend(loc='upper left')
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')

plt.show()

三、柱形图

代码:plt_3.py
文件:unratre.csv
简介:matplotlib库的一些相关操作

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

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])


#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']#评分指标

bar_heights = norm_reviews.ix[0, num_cols].values
#print bar_heights
bar_positions = arange(5) + 0.75#位置
#print bar_positions
fig, ax = plt.subplots()#ax画一个柱状图
ax.bar(bar_positions, bar_heights, 0.5) #0.5表示柱的宽度
plt.show()

#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.ix[0, num_cols].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)
fig, ax = plt.subplots()

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.show()


num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']

bar_widths = norm_reviews.ix[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)#横着操作了

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()

#Let's look at a plot that can help us visualize many points. 散点图
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()

#Switching Axes
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()


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值