网站首页 > 基础教程 正文
作者:潮汐
来源:Python 技术
今天的文章讲解如何利用 Pandas 来绘图,前面写过 matplotlib 相关文章,matplotlib 虽然功能强大,但是 matplotlib 相对而言较为底层,画图时步骤较为繁琐,比较麻烦,因为要画一张完整的图表,需要实现很多的基本组件,比如图像类型、刻度、标题、图例、注解等等。目前有很多的开源框架所实现的绘图功能是基于 matplotlib 的,pandas是其中之一,对于 pandas 数据分析而言,直接使用 pandas 本身实现的绘图方法比 matplotlib 更方便简单。关于更多 Pandas 的相关知识请参考官方文档
Pandas 绘制线状图
使用 Pandas 绘制线状图代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def craw_line():
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
ts = ts.cumsum()
ts.plot()
plt.show()
if __name__ == '__main__':
craw_line()
显示结果如下:
第二种绘画线状图方式如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def craw_line1():
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.cumsum()
df.plot()
plt.show()
if __name__ == '__main__':
craw_line1()
线性图显示结果如下:
Pandas 绘制条形图
除了绘制默认的线状图,还能绘制其他图形样式,例如通过以下方法绘制条形图。绘图方法可以作为plot()的kind关键字参数提供。
绘制条形图1
通过如下方法绘制条形图1,详细代码如下:
def craw_bar():
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
plt.figure()
df.iloc[5].plot(kind="bar")
plt.show()
if __name__ == '__main__':
craw_bar()
结果图显示如下:
绘制条形图2
通过如下方法绘制条形图2,详细代码如下:
def craw_bar1():
#ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
df2 = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df2.plot.bar()
plt.show()
if __name__ == '__main__':
craw_bar1()
图形结果展示如下:
生成堆叠条形图
上面的条形图2可以生成堆叠条形图,加上stacked=True参数即可,详细代码如下:
def craw_bar2():
df2 = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df2.plot.bar(stacked=True)
plt.show()
if __name__ == '__main__':
craw_bar2()
堆叠条形图展示如下:
将以上条形图设置为水平条形图,详细代码如下:
def craw_bar3():
df2 = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
df2.plot.barh(stacked=True)
plt.show()
if __name__ == '__main__':
craw_bar3()
展示结果图如下:
总结
今天的文章就到这里啦,希望今天的文章对大家有帮助!
猜你喜欢
- 2024-11-16 「python实现」01两数之和(python计算两数之和,并写入文件)
- 2024-11-16 python实战技巧之两个字典,如何实现键同值相加「不等长或等长」
- 2024-11-16 Python 基础——运算符之算术运算符
- 2024-11-16 Python函数(python函数怎么写)
- 2024-11-16 Python入门编程题库37--计算每一行的总和、平均值
- 2024-11-16 两分钟掌握Python 函数(python函数教程)
- 2024-11-16 Python中的函数用法(Python中的函数用法)
- 2024-11-16 python元组表达式和方法(python元组的方法)
- 2024-11-16 Python入门编程题库40--列表求和(列表数据求和python)
- 2024-11-16 Python显式循环、列表推导式、sum 函数、集合操作与并行处理用法
- 06-18单例模式谁都会,破坏单例模式听说过吗?
- 06-18Objective-c单例模式的正确写法「藏」
- 06-18单例模式介绍(单例模式都有哪些)
- 06-18前端设计-单例模式在实战中的应用技巧
- 06-18PHP之单例模式(php单例模式连接数据库)
- 06-18设计模式:单例模式及C及C++实现示例
- 06-18python的单例模式(单例 python)
- 06-18你认为最简单的单例模式,东西还挺多
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)