Pandas
是非常常见的数据分析工具,我们一般都会处理好处理数据然后使用searbon
或matplotlib
来进行绘制。但在Pandas
内部就已经集成了matplotlib
,本文将展示Pandas
内部的画图方法。
画图类型
在Pandas
中内置的画图方法如下几类,基本上都是常见的画图方法。每种方法底层也是使用的matplotlib
。
-
line
: line plot (default) -
bar
: vertical bar plot -
barh
: horizontal bar plot -
hist
: histogram -
box
: boxplot -
density/kde
: Density Estimation -
area
: area plot -
pie
: pie plot -
scatter
: scatter plot -
hexbin
: hexbin plot
在进行画图时我们有两种调用方法:
df = pd.DataFrame({
'sales': [3, 3, 3, 9, 10, 6],
'signups': [4, 5, 6, 10, 12, 13],
'visits': [20, 42, 28, 62, 81, 50],
}, index=pd.date_range(start='2018/01/01', end='2018/07/01', freq='M'))
# 方法1,这种方法是高层API,需要制定kind
df.plot(kind='area')
# 方法2,这种方法是底层API
df.plot.area()
面积图(area)
面积图直观地显示定量数据下面的区域面积,该函数包装了 matplotlib 的area函数。
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.area.html
# 默认为面积堆