数据可视化
这里引入python的pylab库,用于绘制图像
import numpy as np
import pylab as pl
x=[1,2,3,4,5]
y=[4,6,9,2,1]
plot1=pl.plot(x,y) #plot函数内可以加参数,如plot(x,y,'--'),绘制虚线图,plot(x,y,'--r')绘制红色虚线,plot(x.y,'o')为散点图
pl.show(plot1)
其中Series和DataFrame两种字典结构也可用plot函数绘制图像
import numpy as np
import pylab as pl
import pandas as pd
a=[1,2,3]
s1=pd.Series(a,index=['a','b','c']) #以index为x轴,Series a为y轴
pl.plot(s1)
pl.show()
import numpy as np
import pylab as pl
import pandas as pd
a={'name':[1,2,50],"age":[17,18,40],'height':[166,167,300]}
s1=pd.DataFrame(a,index=['a','b','c'])
pl.plot(s1)
pl.show()
有时可以在图中添加相应的文字
import numpy as np
import pylab as pl
import pandas as pd
x=[1,2,3,4,5]
y=[1,4,9,16,25]
z=[2,4,6,8,10]
plot1=pl.plot(x,y,'or',label='y')
plot2=pl.plot(x,z,'og',label='z')
pl.legend() #添加图例
pl.title('y vs z') #标题
pl.xlabel=("x axis")
pl.ylabel=("y axis")
pl.show(plot1,plot2)
绘制直方图
import numpy as np
import pylab as pl
import pandas as pd
x=[1,2,3,4,5]
y=[1,4,9,16,25]
z=[2,4,6,8,10]
data=np.random.normal(0,1) #标准正态分布
pl.hist(data)
pl.show()