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()
用excl导入数据文件并用matplotlib画箱线图和提琴图
最新推荐文章于 2024-05-26 20:32:42 发布