pandas中绘图函数df.plot()的使用以及cumsum()函数
df.plot
df.plot(x, y, kind, figsize, title, grid, legend, style)
x 只有dataframe对象时,x可用。横坐标
y 同上,纵坐标变量
kind 可视化图的种类,如下:
| - '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
| - 'scatter' : scatter plot
| - 'hexbin' : hexbin plot.
figsize 画布尺寸
title 标题
grid 是否显示格子线条
legend 是否显示图例
style 图的风格
查看plot参数可以使用help:
1 2 | import pandas as pd help (pd.DataFrame.plot) |
从最简单的开始,如果要绘制一条数据的线性图。因为Series和DataFrame都有一个用于生成各类图表的plot方法。
对于一条Series数据,直接plot()就能获得一张线性图了
1 2 3 4 | data = pd.Series(np.random.randn( 1000 ),index = np.arange( 1000 )) data = data.cumsum() data.plot() plt.show() |
编写以下代码,就可以看到。
对于DataFrame数据也是一样。在生产线型图的代码中,加上kind=‘bar’或kind=‘barh’就可以生成柱状图。
1 2 3 4 5 6 7 8 9 10 11 | '''Series.plot中的参数有很多很多,下图示例中,kind表示那种形式的图表,ax指要在其画板上绘制的对象,也就是图中的那个figure, alpha表示图表填充的透明度。在生产线型图的代码中,加上kind=‘bar’或kind=‘barh’就可以生成柱状图。 在柱状图中,bar表示垂直图,barh表示水平图。''' import pandas as pd import numpy as np import matplotlib.pyplot as plt data = pd.DataFrame(np.random.randn( 10 , 4 ),columns = ( 'a' , 'b' , 'c' , 'd' ),index = (np.arange( 0 , 100 , 10 ))) # data=data.cumsum() data.plot(kind = 'bar' ) #data.plot()如果不加kind='bar’则生成折线图。 plt.show() |
cumsum()可参考:https://blog.csdn.net/LZH_12345/article/details/79848100
# 使用累积求和cumsum()时nan会被忽略。
1 2 3 4 | df = pd.DataFrame([[ 2.0 , 1.0 ],[ 3.0 , np.nan],[ 1.0 , 0.0 ]],columns = list ( 'AB' )) print (df) print (df.cumsum()) # 按列相加 print (df.cumsum(axis = 1 )) # 按行相加 |
可看出:
cumsum()是逐行相加,第二行把第一行的加上,第三行把第一二行的加到一块。
cumsum(axis=1)是逐列相加,第二列把第一列的加上,第三列把第一二列的加到一块。
https://blog.csdn.net/qq_16000815/article/details/80721016
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)