Pandas 的数据可视化使用 matplotlib 为基础组件。
使用jupyter
线型图
Series 和 DataFrame 都提供了一个 plot
的函数。可以直接画出线形图。
import pandas as pd
import numpy as np
## 数据可视化
### 线型图
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
print(ts)
ts.describe()
ts.plot()
ts.plot(title='cumsum', style='r-', ylim=[-30, 30], figsize=(4, 3))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.describe()
df.plot(title='DataFrame cumsum', figsize=(4, 12), subplots=True, sharex=True, sharey=True)
df['I'] = np.arange(len(df))
df.plot(x='I', y=['A', 'C'])
2000-01-01 -1.028299
2000-01-02 -0.266598
2000-01-03 0.449285
2000-01-04 1.478951
2000-01-05 1.645593
2000-01-06 2.696964
2000-01-07 0.989247
2000-01-08 1.381515
2000-01-09 0.847042
2000-01-10 1.687597
2000-01-11 1.700194
2000-01-12 0.772361
2000-01-13 -0.281113
2000-01-14 -0.550958
2000-01-15 0.022021
2000-01-16 -0.402412
2000-01-17 -1.524213
2000-01-18 -2.452963
2000-01-19 -3.390104
2000-01-20 -2.779984
2000-01-21 -1.309700
2000-01-22 -0.594184
2000-01-23 -0.133648
2000-01-24 1.208941
2000-01-25 1.669594
2000-01-26 0.915430
2000-01-27 0.348444
2000-01-28 1.001151
2000-01-29 -0.401387
2000-01-30 -0.760387
…
2002-08-28 -2.163489
2002-08-29 -3.198435
2002-08-30 -4.705796
2002-08-31 -5.265750
2002-09-01 -6.111340
2002-09-02 -4.329813
2002-09-03 -3.163762
2002-09-04 -5.618613
2002-09-05 -6.877079
2002-09-06 -4.386319
2002-09-07 -3.885824
2002-09-08 -3.350432
2002-09-09 -5.217745
2002-09-10 -7.731516
2002-09-11 -9.388807
2002-09-12 -5.877684
2002-09-13 -5.715106
2002-09-14 -5.222551
2002-09-15 -7.285794
2002-09-16 -7.718483
2002-09-17 -8.713023
2002-09-18 -10.261210
2002-09-19 -8.075630
2002-09-20 -10.570523
2002-09-21 -10.461972
2002-09-22 -11.163148
2002-09-23 -11.485168
2002-09-24 -11.034517
2002-09-25 -8.385190
2002-09-26 -9.529578
Freq: D, Length: 1000, dtype: float64
Out[3]:<matplotlib.axes._subplots.AxesSubplot at 0x1ae5b808be0>
柱状图
### 柱状图
df = pd.DataFrame(np.random.rand(10, 4), columns=['A', 'B', 'C', 'D'])
print(df)
df.iloc[1].plot(kind='bar')
df.iloc[1].plot.bar()
df.plot.bar(stacked=True)
df.plot.barh(stacked=True)
直方图
直方图是一种对值频率进行离散化的柱状图。数据点被分到离散的,间隔均匀的区间中,绘制各个区间中数据点的数据。
### 直方图
df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
print(df)
df['a'].plot.hist(bins=10)
df.plot.hist(subplots=True, sharex=True, sharey=True)
df.plot.hist(alpha=0.5)
密度图
正态分布(高斯分布)就是一种自然界中广泛存在密度图。比如我们的身高,我们的财富,我们的智商都符合高斯分布。
#df.std()待调试
### 密度图
df['a'].plot.kde()
df.plot.kde()
df.mean()
带密度估计的规格化直方图
### 带密度估计的规格化直方图
n1 = np.random.normal(0, 1, size=200) # N(0, 1)
n2 = np.random.normal(10, 2, size=200) # N(10, 4)
s = pd.Series(np.concatenate([n1, n2]))
s.describe()
s.plot.hist(bins=100, alpha=0.5, normed=True)
s.plot.kde(style='r-')
散布图
散布图是把所有的点画在同一个坐标轴上的图像。是观察两个一维数据之间关系的有效的手段。
### 散布图
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b');
#散点2
df = pd.DataFrame({'a': np.concatenate([np.random.normal(0, 1, 200), np.random.normal(6, 1, 200)]),
'b': np.concatenate([np.random.normal(10, 2, 200), np.random.normal(0, 2, 200)]),
'c': np.concatenate([np.random.normal(10, 4, 200), np.random.normal(0, 4, 200)])})
df.describe()
df.plot.scatter(x='a', y='b');
饼图
### 饼图
s = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
print(s)
s.plot.pie(figsize=(6,6))
s.plot.pie(labels=['AA', 'BB', 'CC', 'DD'], colors=['r', 'g', 'b', 'c'],
autopct='%.2f', fontsize=20, figsize=(6, 6))
#饼图2
df = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])
print(df)
df.plot.pie(subplots=True,figsize=(9,4))
#df.plot.pie(subplots=True, figsize=(9, 4))
df.plot.pie(figsize=(9, 4))