想要改变世界,就得先改变自己。 ------ 博客首页

3-14 Pandas绘图

 

1.魔法指令:%matplotlib inline ;数据画图

In [1]:
%matplotlib inline 
import pandas as  pd 
In [2]:
import numpy as np
s=pd.Series(np.random.randn(10),index=np.arange(0,100,10))
s.plot()#画图
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x8be4c88>
 
 

2.多个数据画图

In [3]:
df=pd.DataFrame(np.random.randn(10,4).cumsum(0),
            index=np.arange(0,100,10),columns=['A','B','C','D'])#cumsum(0):将数组竖着进行拉平累加,cumsum(1)是横着累加
df.head()
Out[3]:
 
 ABCD
0 -0.798985 0.413034 -1.347718 -1.647887
10 -0.582962 0.386983 -2.000697 -2.115286
20 -0.019045 1.865326 -3.538185 -4.306456
30 -0.181481 0.459417 -2.996726 -2.392947
40 -0.794387 0.641744 -1.513785 -2.305068
In [4]:
df.plot()
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x90c4940>
 
 

3.画子图

In [5]:
import matplotlib.pyplot as plt
fig,axes=plt.subplots(2,1)#构造子图,共2行一列
data=pd.Series(np.random.rand(16),index=list('abcdefghijklmnop'))
data.plot(ax=axes[0],kind='bar')#axes[0]画在第一行;kind='bar'是柱状图
data.plot(ax=axes[1],kind='barh')#axes[0]画在第二行;kind='barh'是横着的柱状图
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x9176b00>
 
 

4.含有图例的柱状图

In [6]:
df=pd.DataFrame(np.random.rand(6,4),
            index=['one','two','three','four','five','six'],
                columns=pd.Index(['A','B','C','D'],name='Genus'))#name='Genus':指定名字
df.head()
Out[6]:
 
GenusABCD
one 0.719657 0.300354 0.913824 0.534699
two 0.060270 0.645280 0.386528 0.837981
three 0.496298 0.312011 0.094697 0.883438
four 0.489973 0.342113 0.063597 0.697251
five 0.531255 0.888178 0.265493 0.173027
In [7]:
df.plot(kind='bar')
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x91a9a20>
 
 

5.直方图hist 指定bins:总共有几条条状图

In [11]:
df.A.plot(kind='hist',bins=50)#A的直方图
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x96a0a90>
 
In [12]:
df.plot(kind='hist',bins=50)#ABCD的直方图
Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0xac81a58>
 
 

6.散点图scatter

  • pd.scatter_matrix:分为两部分,对角线和非对角线(alpha指定透明度)

  • 对角线:核密度估计图(Kernel Density Estimation),就是用来看某 一个 变量分布情况,横轴对应着该变量的值,纵轴对应着该变量的密度(可以理解为出现频次)

  • 非对角线部分:两个变量之间分布的关联散点图。

In [13]:
pd.scatter_matrix(df,color='k',alpha=0.3)
 
E:\software\Anaconda3 5.2.0\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: pandas.scatter_matrix is deprecated, use pandas.plotting.scatter_matrix instead
  """Entry point for launching an IPython kernel.
Out[13]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000054898D0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000097F3CC0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000983D4A8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009863B38>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000009894208>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009894240>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009932F28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000099635F8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000998DC88>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000099BD358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000099E49E8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009A170B8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000009A3D748>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009A5EBE0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009A8D2B0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000009AB6940>]],
      dtype=object)
 
In [20]:
pd.scatter_matrix(df[['A','B']],color='k',alpha=0.5)
 
E:\software\Anaconda3 5.2.0\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: pandas.scatter_matrix is deprecated, use pandas.plotting.scatter_matrix instead
  """Entry point for launching an IPython kernel.
Out[20]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000B53A7F0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000B566BA8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000B597240>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000B5BD8D0>]],
      dtype=object)
 
 

一般的散点图:plot.scatter

In [18]:
df.plot.scatter('A','B')#二维的图
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x9b24898>
 
In [ ]:
 
posted @ 2019-10-15 17:16  karina512  阅读(161)  评论(0编辑  收藏  举报