环境:
- Anaconda for Python3
- Jupyter Notebook
- Chrome
一、数据
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
indx = pd.date_range(2012,periods=7,freq='Y')
s = pd.Series(np.random.randint(1,20,7),index=indx)
df = pd.DataFrame(np.random.randint(1,20,(7,3)),
index=indx,columns=['C1','C2','C3'])
二、最终成果
效果图三、代码
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
column = ['C1','C2','C3']
indx = pd.period_range(2012,periods=7,freq='Y')
s = pd.Series(np.random.randint(1,20,7),index=indx)
df = pd.DataFrame(np.random.randint(1,20,(7,3)),
index=indx,columns=column)
#设置三个子图
fig,axes = plt.subplots(3,1,figsize=(16,9),dpi=300)
#在第一个字图上画Series的柱状图
s.plot(kind='bar',ax=axes[0],
colormap='viridis',
rot=0,
grid=True
)
#在第二个子图上画DataFrame的柱状图
df.plot(kind='bar',ax=axes[1],
colormap='viridis',
rot=0,
grid=True
)
#在第三个子图上画DataFrame的堆叠图
df.plot(kind='bar',ax=axes[2],
colormap='viridis',
rot=0,
grid=True,
stacked=True)
网友评论