用excl导入数据文件并用matplotlib画箱线图和提琴图

import matplotlib.pyplot as plt

import xlrd
import numpy as np
import time
one_time =time.time()

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
data = xlrd.open_workbook(r"E:\工作中的文件\20181119苏焕_数据分析\11-18日全国各个柜机的预留使用率分布情况.xlsx") #打开excel
table = data.sheet_by_name("Sheet1")#读sheet
nrows = table.nrows #获得行数(返回的是行列值中最大的值)
columns = table.ncols
print('行数是:',nrows,'列数是:',columns)
#
first_time = time.time()
print('这是第一小段程序发生的时长:%d s'%(first_time-one_time))
result = []
for i in range(0,columns):
    result_ = []
    for j in range(0,nrows):

        if table.row_values(j)[i]: #这个很重要,因为读取的行数是最大值,于是会有一些空值进来,所以读取的时候得把这些空值个筛选掉。
            result_.append(table.row_values(j)[i])

    step_number = np.where(np.array(result_,dtype=float)>1,1,np.array(result_,dtype=float))
    result.append(np.array(step_number,dtype=float))


fig, axes = plt.subplots(nrows=1, ncols=2,constrained_layout=True)





# plot violin plot




axes[0].violinplot(result,
                   showmeans=False,
                   showmedians=True)
axes[0].set_title('小提琴图')

# plot box plot
axes[1].boxplot(result)
axes[1].set_title('箱型图')

# adding horizontal grid lines
for ax in axes:
    ax.yaxis.grid(True) #x坐标轴的网格使用主刻度
    ax.set_xticks([0.5*(y + 1) for y in range(len(result))])
    # ax.set_xticks([1])

    ax.set_xlabel('日期')
    ax.set_ylabel('预留预约量使用率')

# add x-tick labels
plt.setp(axes, xticks=[x for x in range(1,9)],
         xticklabels=['11日','12日','13日','14日','15日','16日','17日','18日'])
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值