python数据分析基础之图与图表——多图并列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zly412934578/article/details/82987600
#_author:"zhengly"
#date:2018/8/30
'''
除了使用matplotlib创建标准统计图,还可以使用panda来创建其他类型的统计图
本例实现:利用panda创建一个条形图和箱线图,并将它们并排放置
'''
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
#创建一个基础图和两个子图
fig,axes=plt.subplots(nrows=1,ncols=2)
#使用ravel()函数将两个子图分别赋给两个变量ax1和ax2,这样可以避免是使用行和列的索引
ax1,ax2 = axes.ravel()
data_frame = pd.DataFrame(np.random.rand(5,3),index=['Customer 1','Customer 2','Customer 3','Customer 4','Customer 5'],
                          columns=pd.Index(['Metric 1','Metric 2','Metric 3'],name='Metric'))
#创建条形图并设置相关属性
data_frame.plot(kind='bar',ax=ax1,alpha=0.75,title='Bar Plot')
plt.setp(ax1.get_xticklabels(),rotation=45,fontsize=10)
plt.setp(ax1.get_yticklabels(),rotation=0,fontsize=10)
ax1.set_xlabel('Customer')
ax1.set_ylabel('Value')
ax1.xaxis.set_ticks_position('bottom')
ax1.yaxis.set_ticks_position('left')
#创建箱线图并设置相关属性
colors = dict(boxes='DarkBlue',whiskers='Gray',medians='Red',caps='Black')
data_frame.plot(kind='box',color=colors,sym='r.',ax=ax2,title='Box Plot')
plt.setp(ax2.get_xticklabels(),rotation=45,fontsize=10)
plt.setp(ax2.get_yticklabels(),rotation=0,fontsize=10)
ax2.set_xlabel('Metric')
ax2.set_ylabel('Value')
ax2.xaxis.set_ticks_position('bottom')
ax2.yaxis.set_ticks_position('left')
plt.savefig('pandas_plots.png',dpi=400,bbox_inches='tight')
plt.show()

猜你喜欢

转载自blog.csdn.net/zly412934578/article/details/82987600
今日推荐