3-14 Pandas繪圖

 

1.魔法指令:%matplotlib inline ;數據畫圖javascript

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.多個數據畫圖css

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]:
 
  A B C D
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.畫子圖html

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.含有圖例的柱狀圖html5

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]:
 
Genus A B C D
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:總共有幾條條狀圖java

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.散點圖scatternode

  • pd.scatter_matrix:分爲兩部分,對角線和非對角線(alpha指定透明度)python

  • 對角線:核密度估計圖(Kernel Density Estimation),就是用來看某 一個 變量分佈狀況,橫軸對應着該變量的值,縱軸對應着該變量的密度(能夠理解爲出現頻次)jquery

  • 非對角線部分:兩個變量之間分佈的關聯散點圖。linux

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.scatterandroid

In [18]:
df.plot.scatter('A','B')#二維的圖
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x9b24898>
 
In [ ]:
相關文章
相關標籤/搜索