pandas-21 Series和Dataframe的画图方法

前言

在pandas中,无论是series还是dataframe都内置了.plot()方法,可以结合plt.show()进行很方便的画图。

Series.plot() 和 Dataframe.plot()参数

data : Series

kind : str

‘line’ : line plot (default)

‘bar’ : vertical bar plot

‘barh’ : horizontal bar plot

‘hist’ : histogram

‘box’ : boxplot

‘kde’ : Kernel Density Estimation plot

‘density’ : same as ‘kde’

‘area’ : area plot

‘pie’ : pie plot

指定画图的类型,是线形图还是柱状图等

label 添加标签

title 添加标题

……(后接一大堆可选参数)

详情请查阅:[官方文档传送门](http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.plot.html?highlight=series plot#pandas.Series.plot)

Dataframe.plot()参数 也是大同小异:

详情请查阅:官方文档传送门

Series.plot()代码demo

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from pandas import Series # 创建一个随机种子, 把生成的值固定下来
np.random.seed(666) s1 = Series(np.random.randn(1000)).cumsum() s2 = Series(np.random.randn(1000)).cumsum() # series 中 也包含了 plot 方法
s1.plot(kind = 'line', grid = True, label = 'S1', title = 'xxx')
s2.plot(label = 's2') plt.legend()
plt.show() # 图1 # 通过 子图的 方式,可视化 series
figure, ax = plt.subplots(2, 1)
ax[0].plot(s1)
ax[1].plot(s2) plt.legend()
plt.show() # 图2 # 通过 series中的plot方法进行指定是哪一个子图
fig, ax = plt.subplots(2, 1)
s1.plot(ax = ax[1], label = 's1')
s2.plot(ax = ax[0], label = 's2') plt.legend()
plt.show() # 图3
1234567891011121314151617181920212223242526272829303132333435

图1:



图2:



图3:

Dataframe.plot()代码demo

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame np.random.seed(666) df = DataFrame(
np.random.randint(1, 10, 40).reshape(10, 4),
columns = ['A', 'B', 'C', 'D']
)
print(df)
'''
A B C D
0 3 7 5 4
1 2 1 9 8
2 6 3 6 6
3 5 9 5 5
4 1 1 5 1
5 5 6 8 2
6 1 1 7 7
7 1 4 3 3
8 7 1 7 1
9 4 7 4 3
''' # Dataframe 也有个内置方法 plot
df.plot(kind = 'bar') # kind = 'bar'
plt.show() # 图1 # 横向的柱状图
df.plot(kind = 'barh') # kind = 'barh' 可以是一个横向的柱状图
plt.show() # 图2 # 将每个column的柱状图堆叠起来
df.plot(kind = 'bar', stacked = True)
plt.show() # 图3 # 填充的图
df.plot(kind = 'area')
plt.show() # 图4 # 可以进行选择
b = df.iloc[6] # 这时候的b是一个series
b.plot() # 可以看出x轴就是colume的name
plt.show() # 图5 # 可以将所有的行全部画在一张图里
for i in df.index:
df.iloc[i].plot(label = str(i))
plt.legend()
plt.show() # 图6 # 对一列进行画图
df['A'].plot()
plt.show() # 图7
# 多列画图,同上 # 注意:默认是按照column来进行画图的,
# 如果需要按照 index 画图,可以将 dataframe 转置一下
df.T.plot()
plt.show() # 图8
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162

图1:



图2:



图3:



图4:



图5:



图6:



图7:



图8:

pandas-21 Series和Dataframe的画图方法的更多相关文章

  1. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  2. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  3. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  4. 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作

    一.reindex() 方法:重新索引 针对 Series   重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...

  5. pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  6. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  7. Pandas中Series和DataFrame的索引

    在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引.比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字. ...

  8. [Python] Pandas 中 Series 和 DataFrame 的用法笔记

    目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...

  9. Python数据分析-Pandas(Series与DataFrame)

    Pandas介绍: pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. Pandas的主要功能: 1)具备对其功能的数据结构DataFrame.Series 2)集成时间序 ...

随机推荐

  1. VMware下安装Ubuntu虚拟机

    ubuntu系统是以桌面应用为主的.当下最火的linux操作系统,具有实用的界面,并且完全免费. 在Ubuntu的 Linux 世界里,已经不再只是简陋的界面+命令行,而是一款华丽时尚且无比实用的操作 ...

  2. socket套接字及粘包问题

    socket套接字 1.什么是socket socket是一个模块,又称套接字,用来封装互联网协议(应用层以下的层) 2.为什么要有socket 实现应用层以下的层的工作,提高开发效率 3.怎么使用s ...

  3. js数组sort()排序的问题

    最近跟自以为很了解的数组干上了,就像许多我们认知的东西一样,总以为自己很了解的东西,其实并不了解. var a=[12,4,1,43,5,3,52];  alert(a);   //源:12,4,1, ...

  4. 树莓派项目(1-3 )目标识别 NNPACK支持版Darknet,可用于树莓派等嵌入设备

    https://github.com/digitalbrain79/darknet-nnpack https://github.com/AlexeyAB/darknet#how-to-train-to ...

  5. 关于Socket踩过的一些坑

    Socket学习文档  http://developer.51cto.com/developer/javabook/images/3.pdf 1.socket.shutdownOutput(); 文档 ...

  6. Q1094

    一,看题 1,字符串确实是我的弱项. 2, 二,看题解 #include<iostream> #include<string> using namespace std; int ...

  7. 关于三层架构和MVC模式的思考

    MVC模式 核心: 1.解耦Model和View,即使得Model可以被不同的展示,比如一批统计数据可以分别用柱状图.饼图表示 2.Controller用来保证Model和View的同步 Model ...

  8. java web开发入门二(struts)基于eclispe

    JavaBean JavaBean,  咖啡豆. JavaBean是一种开发规范,可以说是一种技术. JavaBean就是一个普通的java类.只有符合以下规定才能称之为javabean: 1)必须提 ...

  9. springboot项目打包成jar/war包

    springboot项目打包过程中包含第三方jar 开发IDE是IntelliJ IDEA,数据库是mysql,内置服务器tomcat. 打包步骤: 1. 确定项目调试运行没问题 2. 将第三方jar ...

  10. 【07月02日】A股滚动市盈率PE最低排名

    ​仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 方大集团(SZ000055) - 滚动市盈率PE:2.68 - 滚动市净率PB:1.2 - 滚动年化股息收益率:3.78% - 建筑产 ...