Python可视化库Matplotlib的使用
一。导入数据
import pandas as pd unrate = pd.read_csv('unrate.csv') unrate['DATE'] = pd.to_datetime(unrate['DATE']) print(unrate.head(12))
结果如下: DATE VALUE 0 1948-01-01 3.4 1 1948-02-01 3.8 2 1948-03-01 4.0 3 1948-04-01 3.9 4 1948-05-01 3.5 5 1948-06-01 3.6 6 1948-07-01 3.6 7 1948-08-01 3.9 8 1948-09-01 3.8 9 1948-10-01 3.7 10 1948-11-01 3.8 11 1948-12-01 4.0二。使用Matplotlib库
import matplotlib.pyplot as plt #%matplotlib inline #Using the different pyplot functions, we can create, customize, and display a plot. For example, we can use 2 functions to : plt.plot() plt.show()
结果如下:
三。插入数据
first_twelve = unrate[0:12] plt.plot(first_twelve['DATE'], first_twelve['VALUE']) plt.show()
由于x轴过于紧凑,所以使用旋转x轴的方法 结果如下。
plt.plot(first_twelve['DATE'], first_twelve['VALUE']) plt.xticks(rotation=45) #print help(plt.xticks) plt.show()
四。设置x轴y轴说明
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()
五。子图设置
import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(4,3,1) ax2 = fig.add_subplot(4,3,2) ax2 = fig.add_subplot(4,3,6) plt.show()
六。一个图标多个曲线。
1.简单实验。
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()
2.使用循环
fig = plt.figure(figsize=(10,6)) colors = ['red', 'blue', 'green', 'orange', 'black'] for i in range(5):start_index = i*12end_index = (i+1)*12subset = unrate[start_index:end_index]plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i])plt.show()
3.设置标签
fig = plt.figure(figsize=(10,6)) colors = ['red', 'blue', 'green', 'orange', 'black'] for i in range(5):start_index = i*12end_index = (i+1)*12subset = unrate[start_index:end_index]label = str(1948 + i)plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label) plt.legend(loc='best') #print help(plt.legend) plt.show()
4。设置完整标签
fig = plt.figure(figsize=(10,6)) colors = ['red', 'blue', 'green', 'orange', 'black'] for i in range(5):start_index = i*12end_index = (i+1)*12subset = 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()
七。折线图(某电影评分网站)
1.读取数据
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[:10])
2.设置说明
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.bar绘制折线图,bar_positions绘制离远点的距离,0.5绘制离折线图的宽度。 ax.set_xticks(tick_positions) ax.set_xticklabels(num_cols, rotation=45)//横轴的说明 旋转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()
3.旋转x轴 y轴
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.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()
八。 散点图
1。基本散点图
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()
2.拆分散点图
#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()
Ps:还是呈现很强的相关性的,基本呈直线分布
九。直方图
1.读入数据
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'] norm_reviews = reviews[cols] print(norm_reviews[:100])
2.统计评分个数
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()print(fandango_distribution) print(imdb_distribution)
3.画直方图
fig, ax = plt.subplots() #ax.hist(norm_reviews['Fandango_Ratingvalue']) #ax.hist(norm_reviews['Fandango_Ratingvalue'],bins=20) ax.hist(norm_reviews['Fandango_Ratingvalue'], range=(4, 5),bins=20)//划分的区间20个,只统计4-5区间的bins plt.show()
4.不同的媒体评分图
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(norm_reviews['Fandango_Ratingvalue'], bins=20, range=(0, 5)) ax1.set_title('Distribution of Fandango Ratings') 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()
5.四分图
fig, ax = plt.subplots() ax.boxplot(norm_reviews['RT_user_norm']) ax.set_xticklabels(['Rotten Tomatoes']) ax.set_ylim(0, 5) plt.show()
ps:四分图就是1/4,2/4,3/4的点是多少,可以看到大致的范围
6.四家媒体四方图
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.show()
转载于:https://www.cnblogs.com/LHWorldBlog/p/7819331.html
Python可视化库Matplotlib的使用相关推荐
- Python可视化库Matplotlib绘图入门详解
Matplotlib是Python的绘图库,其中的pyplot包封装了很多画图的函数. Matplotlib.pyplot 包含一系列类似 MATLAB 中绘图函数的相关函数.每个 Matplotli ...
- Python可视化库matplotlib.pyplot里contour与contourf的区别
contour和contourf都是画三维等高线图的,不同点在于contourf会对等高线间的区域进行填充,区别如下: import numpy as np import matplotlib.pyp ...
- python画学校_未明学院:Python可视化库Matplotlib绘图入门详解
Matplotlib是Python的绘图库,其中的pyplot包封装了很多画图的函数. Matplotlib.pyplot 包含一系列类似 MATLAB 中绘图函数的相关函数.每个 Matplotli ...
- Python可视化库matplotlib(基础整理)
https://blog.csdn.net/weixin_39777626/article/details/78598346 绘制基本曲线使用plot函数绘制函数曲线,可以调整plot函数参数配置曲线 ...
- Python可视化库matplotlib(超详细)
超详细Matplotlib笔记 Matplotlib简介 开发环境搭建 为什么要学习Matplotlib 绘制基础 图形绘制流程 认识Matplotlib图像结构 实现基础绘图功能 设置标签文字和线条 ...
- python可视化库总结_Python 可视化库 - Matplotlib 使用总结
Python 可视化库 - Matplotlib 使用总结 在做完数据分析后, 有时候需要将分析结果一目了然地展示出来, 此时便离不开 Python 可视化工具, Matplotlib 是 Pytho ...
- [转载] Python数据可视化库-Matplotlib——折线图绘制
参考链接: Python Matplotlib数据可视化 plot折线图 # coding:utf-8 import pandas as pd import numpy as np from matp ...
- python绘制不带颜色曲线图_Python数据可视化库-Matplotlib——折线图,子图绘制
# coding:utf-8 import pandas as pd import numpy as np from matplotlib import pylab as plt # 导入数据可视化库 ...
- 新视角带你认识Python可视化库(附代码资源)
作者:Dan Saber 翻译:笪洁琼 校对:梁傅淇 本文约16196字,建议阅读20+分钟. 本文中,作者借助拟人化的形式,让Python中值得一提的可视化库共同出演了一出戏剧,形象.生动地展现了不 ...
最新文章
- Java项目:资源下载工具(java+swing)
- Android与H5交互
- go网站服务器搭建,使用Go搭建服务器记录(一)
- 用netty实现zcool_《Netty官方指南》把Netty当做一个通用的库
- IE/Firefox中css兼容常见问题
- 数字图像处理实验(7):PROJECT 04-03 , Lowpass Filtering
- HTML abbr元素
- 三维旋转四元数系列(2.三维旋转之轴角与罗德里格斯公式推导)
- PostgreSQL在何处处理 sql查询之十四
- ORACLE 视图的 with check option
- 第九章 JSP标签——《跟我学Shiro》[张开涛]
- openwrt 公板硬件方案介绍
- python表示倍数_python代表倍数
- 苹果如何修改无线DNS服务器,苹果手机修改无线wifi的DNS是否能够提升网速?
- 投入产出表matlab,投入产出分析投入产出表.doc
- 计算机本地硬盘带蓝色问号,win10本地磁盘显示蓝色问号该怎么解决
- 燕山大学联通新卡绑定校园网
- intellij idea cpu占用率太大太满 运行速度太慢 使了五个解决方法最终成功
- 从信息传播角度来看链路预测
- Linux环境安装unzip
热门文章
- Java中的AtomicInteger
- python scikit_Python SciKit学习教程
- jquery 遍历无限极树_jQuery parent()和children()树遍历函数示例
- 无法使用struts2注释_带有注释且没有struts.xml文件的Struts 2 Hello World示例
- primefaces教程_Primefaces Spring和Hibernate集成示例教程
- Java基础之字符串详细比较
- js进阶 10-4 jquery中基础选择器有哪些
- Chapter 1. Asp.Net 概述
- Linux 下安装Postgresql
- 就算神游 之五:东京迪斯尼乐园 1