fuli2020

2020-07-20   阅读量: 729

Pandas 绘图

扫码加入数据分析学习群

绘图的两种方法:

第一种方法:

%matplotlib inline

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

s=pd.Series([909976,8615246,2872086,2273305],index=['Stockholm','London','Rome','Paris'],name='Population')

fig,axes=plt.subplots(1,2,figsize=(12,3))

s.plot(ax=axes[0],kind='line',title='line') #线图

s.plot(ax=axes[1],kind='bar',title='bar') #柱状图

fig.tight_layout() #自动调整格式


第二种方法:

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.bar()#可进行堆叠的bar
plt.show()


#线性图

df = pd.DataFrame({
'pig': [20, 18, 489, 675, 1776],
'horse': [4, 25, 281, 600, 1900]
}, index=[1990, 1997, 2003, 2009, 2014])
df.plot.line()
plt.show()


#柱状图

speed=[0,1,17.5,40,48,52,69,88]

lifespan=[2,8,70,1.5,25,12,28]

index=['snail','pig','elephant','rabbit','giraffe','coyote','horse']

df=pd.DataFrame({'speed':speed,'lifespan':lifespan},index=index)

ax=df.plot.bar(rot=30,figsize=(10,5))


#散点图

df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
[6.4, 3.2, 1], [5.9, 3.0, 2]],
columns=['length', 'width', 'species'])
ax1 = df.plot.scatter(x='length',
y='width',
c='r',figsize=(10,5))


#直方图

data=np.random.randn(25,4)
df=pd.DataFrame(data,columns=list('ABCD'))
ax=df.plot.hist(bins=20,alpha=0.5)


#线箱图

data=np.random.randn(25,4)
df=pd.DataFrame(data,columns=list('ABCD'))
ax=df.plot.box()


#饼图

df=pd.DataFrame({'mass':[0.330,4.87,5.97],'radius':[2439.7,6051.8,6378.1]},index=['Mercurry','Venus','Earth'])
df.plot.pie(y='mass',figsize=(5,5))



27.9786 3 3 关注作者 收藏

评论(0)


暂无数据

推荐课程