pandas库中数据结构DataFrame的绘制函数

2024-05-30 03:48

本文主要是介绍pandas库中数据结构DataFrame的绘制函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在使用Canopy进行数据分析时,我们会用到pandas库,通过它我们可以灵活的对数据进行处理、转换和绘图等操作。其中非常重要的数据结构就是DataFrame。
本文主要整理一下对DataFrame对象进行plot操作的使用说明。

函数名称:
pandas.DataFrame.plot

函数参数列表及缺省值:
DataFrame.plot(data, x=None, y=None, kind=’line’, ax=None, subplots=False, sharex=True, sharey=False, layout=None, figsize=None, use_index=True, title=None, grid=None, legend=True, style=None, logx=False, logy=False, loglog=False, xticks=None, yticks=None, xlim=None, ylim=None, rot=None, fontsize=None, colormap=None, table=False, yerr=None, xerr=None, secondary_y=False, sort_columns=False, **kwds)

Make plots of DataFrame using matplotlib / pylab.

相关参数说明:

Parameters :

data : DataFrame

# 需要绘制的数据对象

x : label or position, default None

# x轴的标签值,缺省值是 None   

y : label or position, default None

Allows plotting of one column versus another

# y轴的标签值,缺省值是 None

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

‘scatter’ : scatter plot

‘hexbin’ : hexbin plot

# 标识绘制方式的字符串,缺省值是 ‘line’ 

ax : matplotlib axes object, default None

# 当使用到subplots绘图时,会得到包含子图对象的参数,

    再完善子图内容时需要指定该参数,缺省值是 None [可参照后面示例1]

subplots : boolean, default False

Make separate subplots for each column

# 所绘制对象数据 data 是否需要分成不同的子图, 缺省值是 False  [可参照后面示例2]

sharex : boolean, default True

In case subplots=True, share x axis

# 当参数subplots 为 True时,该值表示各子图是否共享x轴标签值,缺省值是 True

sharey : boolean, default False

In case subplots=True, share y axis

# 当参数subplots 为 True时,该值表示各子图是否共享x轴标签值,缺省值为 True

layout : tuple (optional)

(rows, columns) for the layout of subplots

figsize : a tuple (width, height) in inches

use_index : boolean, default True

Use index as ticks for x axis

title : string

# 图的标题

Title to use for the plot

grid : boolean, default None (matlab style default)

Axis grid lines

# 是否需要显示网格,缺省值是 None[需要留意的是,在Canopy中默认是显示网格的]

legend : False/True/’reverse’

Place legend on axis subplots

# 添加子图的图例,缺省值是True

style : list or dict

matplotlib line style per column

# 设置绘制线条格式,仅当参数kind 设置为 ‘line’ [可参照后面示例3]

logx : boolean, default False

Use log scaling on x axis

# 将x轴设置成对数坐标,缺省值是False

logy : boolean, default False

Use log scaling on y axis

# 将y轴设置成对数坐标,缺省值是False

loglog : boolean, default False

Use log scaling on both x and y axes

# 将x轴、y轴都设置成对数坐标,缺省值是False

xticks : sequence

Values to use for the xticks

# 指定 x轴标签的取值范围(或步长)

yticks : sequence

Values to use for the yticks

# 指定 y轴标签的取值范围(或步长)

xlim : 2-tuple/list

ylim : 2-tuple/list

rot : int, default None

Rotation for ticks (xticks for vertical,

yticks for horizontal plots)

fontsize : int, default None

Font size for xticks and yticks

# 字体大小,缺省值是 None

colormap : str or matplotlib colormap object,

default None

Colormap to select colors from. If string,

load colormap with that name from matplotlib.

# 指定具体颜色取值或对应对象名称,缺省值是 None

colorbar : boolean, optional

If True, plot colorbar (only relevant for

‘scatter’ and ‘hexbin’ plots)

# 是否显示颜色条,如果设为 True,则仅当参数kind 设置为 ‘scatter’、 ‘hexbin’时有效

position : float

Specify relative alignments for bar plot layout.

From 0 (left/bottom-end) to 1 (right/top-end).

Default is 0.5 (center)

layout : tuple (optional)

(rows, columns) for the layout of the plot

table : boolean, Series or DataFrame, default False

If True, draw a table using the data in the

DataFrame and the data will be transposed to meet

matplotlib’s default layout. If a Series or

DataFrame is passed, use passed data to draw a table.

yerr : DataFrame, Series, array-like, dict and str

See Plotting with Error Bars for detail.

xerr : same types as yerr.

stacked : boolean, default False in line and

bar plots, and True in area plot.

If True, create stacked plot.

# 参数kind 设置为 ‘line’、’bar’时,该值默认为False,

# 参数 kind 设置为’area’时,该值默认为True

# 当该参数设置为True时,生成对应的堆积图

sort_columns : boolean, default False

Sort column names to determine plot ordering

secondary_y : boolean or sequence, default False

Whether to plot on the secondary y-axis If a list/tuple,

which columns to plot on secondary y-axis

mark_right : boolean, default True

When using a secondary_y axis, automatically mark the

column labels with “(right)” in the legend

kwds : keywords

Options to pass to matplotlib plotting method

Returns :      axes : matplotlib.AxesSubplot or np.array of them

示例:

示例1:

参照《Python 数据分析(三)[MAC]》中第7个分析任务—男孩女孩名字中各个末字母的比例

1 <pre>### 7. count the sum of each last letter of names of diferent 'sex' in each year
2 get_last_letter = lambda x: x[-1]
3 last_letters = names.name.map(get_last_letter)
4 last_letters.name = 'last_letter'
5 table = names.pivot_table('births', rows=last_letters, cols=['sex''year'], aggfunc=sum)
6 subtable = table.reindex(columns=[191019602010], level='year')
7 letter_prop = subtable / subtable.sum().astype(float)
8 import matplotlib.pyplot as plt
9 fig, axes = plt.subplots(21, figsize=(108))
10 letter_prop['M'].plot(kind='bar', rot=0, ax=axes[0], title='Male')
11 letter_prop['F'].plot(kind='bar', rot=0, ax=axes[1], title='Female', legend=False)

说明:
先调用plt(matplotlib.pyplot)绘制两个空白子图,再通过对应数据对象(letter_prop[‘M’]、letter_prop[‘F’])依次绘制不同的子图,这时调用plot函数时就需要设置ax参数,让其指定到待显示的子图对象。对应绘制结果如下。

 

示例2:

参照《Python 数据分析(三)[MAC]》中第4个分析任务—获取’John’、’Harry’、’Mary’、’Marilyn’随时间变化的使用数量

1 ### 4. get the sum of names['John', 'Harry', 'Mary', 'Marilyn'] of diferent 'sex' in each year
2 subset = total_births[['John''Harry''Mary''Marilyn']]
3 subset.plot(subplots=True, figsize=(1210), grid=False, title="Number of births per year", xticks=range(1880202010))

说明:
先获取含有不同子图所涉及的数据对象subset,再在调用plot时,设置参数 subplots = True。对应绘制结果如下。

示例3:

参照《Python 数据分析(三)[MAC]》中第4个分析任务—各年度使用包含’lesl’名字的男女比例

1 ### 9. count the ratio of names[contain 'lesl'] of diferent 'sex' in each year
2 all_names = top1000.name.unique()
3 mask = np.array(['lesl' in x.lower() for in all_names])
4 lesley_like = all_names[mask]
5 filtered = top1000[top1000.name.isin(lesley_like)]
6 filtered.groupby('name').births.sum()
7 table = filtered.pivot_table('births', rows='year', cols='sex', aggfunc='sum')
8 table = table.div(table.sum(1), axis=0)
9 table.plot(style={'M':'k-','F''k--'}, xticks=range(1880202010))

说明:
对同一个图形中的两条直线使用不同的图形,style={‘M':’k-‘,’F': ‘k–‘}。对应绘制结果如下。

>>  本文链接:  https://www.jelekinn.com/pandas-dataframe-plot.html
>>  转载请注明来源:  Jele Kinn’s Blog >>  《pandas库中数据结构DataFrame的绘制函数》

这篇关于pandas库中数据结构DataFrame的绘制函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1015496

相关文章

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Python的pandas库基础知识超详细教程

《Python的pandas库基础知识超详细教程》Pandas是Python数据处理核心库,提供Series和DataFrame结构,支持CSV/Excel/SQL等数据源导入及清洗、合并、统计等功能... 目录一、配置环境二、序列和数据表2.1 初始化2.2  获取数值2.3 获取索引2.4 索引取内容2

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Python Excel 通用筛选函数的实现

《PythonExcel通用筛选函数的实现》本文主要介绍了PythonExcel通用筛选函数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录案例目的示例数据假定数据来源是字典优化:通用CSV数据处理函数使用说明使用示例注意事项案例目的第一

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法