Matplotlib + Seaborn - 绘图基础

这篇随笔主要介绍利用 Matplotlib 和 Seaborn 绘制基本图像,包括折线图、条形图、直方图、密度图、散点图、散步矩阵图等;最后还简单介绍了 Seaborn 分面网格的功能

1
2
3
4
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
1
%matplotlib inline

obj.plot( use_index, xlim, xticks, xlabel, rot, logy, figsize, label, legend, title, kind, style, alpha, grid, ax )

use_index : 是否将对象索引用作刻度标签
xlim : x轴界限, xticks : x轴刻度值, xlabel : x轴名称
logy : 是否在y轴使用对数标尺( log(p) → h
label, legend : 图例
kind : 控制画图种类, 如: 'line', 'bar', 'barh', 'kde'
style : 控制画图属性, 如: 'ko--'
grid : 是否显示网格线
ax : 指定要在其上进行绘制的 subplot 对象

1
2
3
fig, axes = plt.subplots(1,1)
s = pd.Series(np.array([1,2,3,4]))
s.plot(kind='bar', grid=True, rot=0)
1
<AxesSubplot:>

png

frame.plot( ... ..., subplots, sharex, sharey, sortcolumns )

subplots : 是否将 frame 各列单独绘制
sharex : subplots=True : 是否共用一个 x 轴, sharey : subplots=True : 是否共用一个 y 轴
sort_columns : 是否以字母表顺序绘制各列

1
2
3
4
df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
columns=['A', 'B', 'C', 'D'],
index=np.arange(0, 100, 10))
df.plot()
1
<AxesSubplot:>

png

1
df.plot(subplots=True)
1
2
array([<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
dtype=object)

png

frame.plot.line( ... ... ) : 折线图

1
2
3
4
5
6
7
8
fig, axes = plt.subplots(2, 1)
fig.subplots_adjust()
data = pd.DataFrame(np.random.rand(6, 4),
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'],
name='Genus'))
data.plot.bar(ax=axes[0])
data.plot.line(ax=axes[1])
1
<AxesSubplot:>

png

frame.plot.bar( stacked, ... ), frame.plot.barh( stacked, ... ) : 条形图

1
2
3
4
5
data = pd.DataFrame(np.arange(1,25).reshape(4, 6).T, 
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'],
name='Genus'))
data
GenusABCD
one171319
two281420
three391521
four4101622
five5111723
six6121824
1
2
data.plot.bar()
data.plot.barh(stacked=True) #叠加
1
<AxesSubplot:>

png

png

sns.barplot( data, x, y, hue, orient ) : 平均值及置信度

1
2
tips = pd.read_csv('pydata-book-2nd-edition/examples/tips.csv')
tips.head()
total_billtipsmokerdaytimesize
016.991.01NoSunDinner2
110.341.66NoSunDinner3
221.013.50NoSunDinner3
323.683.31NoSunDinner2
424.593.61NoSunDinner4
1
2
tips['tip/total_bill'] = tips['tip'] / tips['total_bill']
sns.barplot(data=tips, x='tip/total_bill', y='day', orient='h')
1
2
3
4
5
6
7
8
9
10
11
<AxesSubplot:xlabel='tip/total_bill', ylabel='day'>
````

![png](08-pandas_and_seaborn/08-pandas_and_seaborn_15_1.png)

### ***frame.plot.hist( bins, ... ), frame.plot.density( ... )*** : 直方图和密度图

```python
tips = pd.read_csv('pydata-book-2nd-edition/examples/tips.csv')
tips['tip/total_bill'] = tips['tip'] / tips['total_bill']
tips.head()
total_billtipsmokerdaytimesizetip/total_bill
016.991.01NoSunDinner20.059447
110.341.66NoSunDinner30.160542
221.013.50NoSunDinner30.166587
323.683.31NoSunDinner20.139780
424.593.61NoSunDinner40.146808
1
tips['tip/total_bill'].plot.hist(bins=50)
1
<AxesSubplot:ylabel='Frequency'>

png

1
tips['tip/total_bill'].plot.density()
1
<AxesSubplot:ylabel='Density'>

png

sns.regplot( data, x, y, hue ) : 散点图

1
2
3
4
5
macro = pd.read_csv('pydata-book-2nd-edition/examples/macrodata.csv')
data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]
trans_data = np.log(data).diff().dropna()
trans_data.head()
sns.regplot(x = 'm1', y = 'unemp', data=trans_data)
1
<AxesSubplot:xlabel='m1', ylabel='unemp'>

png

sns.pairplot( data, diag_kind, plot_kws ) : 散步图矩阵

1
sns.pairplot(trans_data, diag_kind='kde')
1
<seaborn.axisgrid.PairGrid at 0x2244980c220>

png

sns.catplot( data, x, y, hue, row, col, kind ) : 分面网格

1
2
3
4
tips = pd.read_csv('pydata-book-2nd-edition/examples/tips.csv')
tips['tip_pct'] = tips['tip'] / tips['total_bill']
sns.catplot(x='day', y='tip_pct', hue='time', col='smoker',
kind='bar', data=tips)
1
<seaborn.axisgrid.FacetGrid at 0x224497158b0>

png

1
2
3
sns.catplot(x='day', y='tip_pct', row='time',
col='smoker',
kind='bar', data=tips)
1
<seaborn.axisgrid.FacetGrid at 0x2244a232370>

png

1
2
sns.catplot(x='tip_pct', y='day', kind='bar',
data=tips[tips.tip_pct < 0.5])
1
<seaborn.axisgrid.FacetGrid at 0x2244a653100>

png

1
2
sns.catplot(x='tip_pct', y='day', kind='box',
data=tips[tips.tip_pct < 0.5])
1
<seaborn.axisgrid.FacetGrid at 0x2244a830af0>

png