tynbl.github.io

Pandas及Seaborn绘图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

%matplotlib notebook

1. Pandas绘图

# 可用的绘图样式 
plt.style.available
['seaborn-dark-palette',
 'seaborn-muted',
 'seaborn-whitegrid',
 'seaborn-colorblind',
 'seaborn',
 'seaborn-pastel',
 'seaborn-darkgrid',
 'seaborn-bright',
 'seaborn-paper',
 'ggplot',
 'dark_background',
 'seaborn-talk',
 'fivethirtyeight',
 '_classic_test',
 'bmh',
 'seaborn-notebook',
 'seaborn-poster',
 'seaborn-deep',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-dark',
 'grayscale',
 'classic']
# 设置绘图样式
plt.style.use('seaborn-colorblind')

DataFrame绘图

np.random.seed(100)
df = pd.DataFrame({'A': np.random.randn(365).cumsum(0),
                  'B': np.random.randn(365).cumsum(0) + 20,
                  'C': np.random.randn(365).cumsum(0) - 20},
                 index=pd.date_range('2017/1/1', periods=365))
df.head()
A B C
2017-01-01 -1.749765 21.091816 -19.638975
2017-01-02 -1.407085 20.686733 -17.061997
2017-01-03 -0.254049 21.223177 -15.474676
2017-01-04 -0.506485 19.091862 -14.786998
2017-01-05 0.474835 19.354939 -14.805210
df.plot()
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0x9d6b518>
df.plot('A', 'B', kind='scatter')
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0x63e0550>
# 颜色(c)和大小(s)有'B'列的数据决定
ax = df.plot('A', 'C', kind='scatter',
        c='B', s=df['B'], colormap='viridis')
<IPython.core.display.Javascript object>

C:\Anaconda2\envs\py35\lib\site-packages\matplotlib\collections.py:877: RuntimeWarning: invalid value encountered in sqrt
  scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
# 设置坐标为相同比例
ax.set_aspect('equal')
df.plot(kind='box')
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0xb7fb208>
df.plot(kind='hist', alpha=0.7)
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0xceca438>
df.plot(kind='kde')
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0xc5b9780>

pandas.tools.plotting

iris = pd.read_csv('iris.csv')
iris.head()
SepalLength SepalWidth PetalLength PetalWidth Name
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
# 用于查看变量间的关系
pd.plotting.scatter_matrix(iris)
<IPython.core.display.Javascript object>

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000F79EBA8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001038CA90>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000103DABE0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001041C860>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000010469D68>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000104AB5C0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000104F9710>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001053CF60>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000001058F3C8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000105D2668>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001061FB70>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000106654A8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000011680898>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000D31B550>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000116E3AC8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000011731A20>]], dtype=object)
# 用于查看多遍量分布
plt.figure()
pd.plotting.parallel_coordinates(iris, 'Name')
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0x11718898>

2. Seaborn绘图

import seaborn as sns
np.random.seed(100)
v1 = pd.Series(np.random.normal(0, 10, 1000), name='v1')
v2 = pd.Series(2 * v1 + np.random.normal(60, 15, 1000), name='v2')
# 通过matplotlib绘图
plt.figure()
plt.hist(v1, alpha=0.7, bins=np.arange(-50, 150, 5), label='v1')
plt.hist(v2, alpha=0.7, bins=np.arange(-50, 150, 5), label='v2')
plt.legend()
<IPython.core.display.Javascript object>

<matplotlib.legend.Legend at 0x1270d5c0>
plt.figure()
plt.hist([v1, v2], histtype='barstacked', normed=True)
v3 = np.concatenate((v1, v2))
sns.kdeplot(v3)
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0x134566a0>
# 使用seaborn绘图
plt.figure()
sns.distplot(v3)
<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0x13be8240>
# 使用seaborn绘图
plt.figure()
sns.jointplot(v1, v2, alpha=0.4)
<IPython.core.display.Javascript object>

<IPython.core.display.Javascript object>

<seaborn.axisgrid.JointGrid at 0x1437b8d0>
# 使用seaborn绘图
plt.figure()
grid = sns.jointplot(v1, v2, alpha=0.4)
grid.ax_joint.set_aspect('equal')
<IPython.core.display.Javascript object>

<IPython.core.display.Javascript object>

plt.figure()
sns.jointplot(v1, v2, kind='hex')
<IPython.core.display.Javascript object>

<IPython.core.display.Javascript object>

<seaborn.axisgrid.JointGrid at 0x166f1438>
plt.figure()
sns.jointplot(v1, v2, kind='kde')
<IPython.core.display.Javascript object>

<IPython.core.display.Javascript object>

<seaborn.axisgrid.JointGrid at 0x16efa6a0>
iris = pd.read_csv('iris.csv')
iris.head()
SepalLength SepalWidth PetalLength PetalWidth Name
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
sns.pairplot(iris, hue='Name', diag_kind='kde')
<IPython.core.display.Javascript object>

<seaborn.axisgrid.PairGrid at 0x133a4ba8>
plt.figure()
plt.subplot(121)
sns.swarmplot('Name', 'PetalLength', data=iris)
plt.subplot(122)
sns.violinplot('Name', 'PetalLength', data=iris)
C:\Anaconda2\envs\py35\lib\site-packages\matplotlib\pyplot.py:524: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)



<IPython.core.display.Javascript object>

<matplotlib.axes._subplots.AxesSubplot at 0x144e4400>