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]:
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]:
In [4]:
df.plot()
Out[4]:
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]:
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]:
In [7]:
df.plot(kind='bar')
Out[7]:
5.直方图hist 指定bins:总共有几条条状图
In [11]:
df.A.plot(kind='hist',bins=50)#A的直方图
Out[11]:
In [12]:
df.plot(kind='hist',bins=50)#ABCD的直方图
Out[12]:
6.散点图scatter
-
pd.scatter_matrix:分为两部分,对角线和非对角线(alpha指定透明度)
-
对角线:核密度估计图(Kernel Density Estimation),就是用来看某 一个 变量分布情况,横轴对应着该变量的值,纵轴对应着该变量的密度(可以理解为出现频次)
-
非对角线部分:两个变量之间分布的关联散点图。
In [13]:
pd.scatter_matrix(df,color='k',alpha=0.3)
Out[13]:
In [20]:
pd.scatter_matrix(df[['A','B']],color='k',alpha=0.5)
Out[20]:
一般的散点图:plot.scatter
In [18]:
df.plot.scatter('A','B')#二维的图
Out[18]:
In [ ]:
此资源来自https://www.cnblogs.com/AI-robort/,博客园的karina512。