1.设置Matplotlib字体为黑体:
matplotlib.rcParams['font.family'] = 'SimHei'
2.按照某一列进行聚类,并提出某一类:
比如dataframe有一列为 [a,b,c,c,d,a,a,b,b],按照a,b,c,d对该dataframe进行聚类,并提取a列:
list=np.array(df.loc[:,'abc'].unique() ##某一列unique
df_1=df.groupby([list]) ##按照list进行聚类
df_1.get_group('a') ##提取a这一列
3.append()合并dataframe,并不会将几个dataframe合并成一个二维数组,而是合并成一个多维数组;例:
a=[2344555]
b=[1244455]
c=[112233]
a.append(b)
a.append(c)
print(a)
输出结果:a:[2344555, [1244455], [112233]]
4.try expect 异常处理
try:
<语句体>
except(ValueError): #要捕捉的错误
<出现错误之后执行的语句体> #取出这5000米的最大SOH
a.append(np.nan) #直接赋空值
5.求最大最小值和均值
max(dataframe[])
min(dataframe[])
dataframe[].mean()
6.matplotlib画水平线和竖线
plt.vlines(x,y_min,y_max)##输入x坐标值、y最小值、y最大值
plt.hlines(y,x_min,x_max)##输入y坐标值,最小x值和最大x值
7.绘制默认箱型图
plt.boxplot(x=data_all, #用append填充的dataframe数组可直接扔进去
patch_artist=True, #是否自定义箱型图颜色?是
labels=labels, #x轴标签
boxprops={'color': 'black', 'facecolor': '#9999ff'}, #设置箱体颜色和边线颜色
# 中位数显示方案,线性及颜色
# medianprops={'linestyle': '--', 'color': 'orange'},
# 均值显示方案
showmeans=True, #显示均值点
medianprops={'linestyle': ' ', 'color': 'orange'}, #将中位数线性设置为空格,间接让中位数不显示
meanline=True, #以线的形式还是点的形式显示均值点:是则为线
meanprops={'linestyle': '--', 'color': 'orange'}, #设置均值点线性
# meanprops={'marker': 'D', 'markerfacecolor': 'indianred'}, #以点显示均值时中位数形式设置
#设置离群点
showfliers=False, #是否显示异常值点:否
# flierprops = {'marker': 'o', 'markerfacecolor': 'red', 'color': 'black'}, #设置离群点样式
#其他属性值设置
# whis=5, #设置上下异常边缘到四分位数的距离
# notch = True, #是否以凹凸形式显示中位数
whiskerprops={'linestyle':' '}, #设置须的属性 以最大最小SOH为界时需设置为空格,不显示须
capprops={'linestyle':' '} #设置上下限的属性 以最大最小SOH为界时需设置为空格,不显示上下限
)
8.设置x,y轴刻度的精度
x_major_locator = plt.MultipleLocator(1)
# # 把x轴的刻度间隔设置为1,并存在变量里
y_major_locator = plt.MultipleLocator(2)
#把x轴的刻度间隔设置为2,并存在变量里
ax = plt.gca()
# # ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator)
# # 把x轴的主刻度设置为1的倍数
ax.yaxis.set_major_locator(y_major_locator)
# # 把y轴的主刻度设置为2的倍数
9.将数据导出excel中的多个sheet
data_excel['x_mile']=labels
data_excel['x_max'] = data_all_max
data_excel['x_min'] = data_all_min
data_excel['x_mean'] = data_all_mean
data_excel['x_a1']=data_all_a1
data_excel['x_a2']=data_all_a2
data_excel['num']=vehicle_model_code_list[i]
data_excel['labels']=labels
for i in np.arange(0,len(...))
sh = 'sheet{}'.format(i)
print(sh)
writer = pd.ExcelWriter('D:\电源系统-大数据\大数据\SOH箱型图/箱型图.xlsx',engine='openpyxl')
book= openpyxl.load_workbook(writer.path) #给出excel路径
writer.book=book
df = pd.DataFrame(data_excel) # 假设这里每次更新不同的df
df.to_excel(writer, sheet_name=sh) #将data_excel写入excel文件对应的sheet里,sheet是自动创建的
writer.save()
writer.close()
10.matplotlib绘制多个长方形
ax = plt.gca()
for n in np.arange(0,len(data_all_a1)): #for循环绘制多个长方形
ax.add_patch(plt.Rectangle(xy=(data_x[n], data_y[n]),##这里放入左下角的x和y值
width=0.25, ##这里放入长方形的宽
height=height[n], ##这里放入长方形的高
fill='blue', #设置长方形填充颜色
edgecolor='black', #设置长方形边缘颜色
linewidth=1)) #设置长方形边线宽度
# plt.xlim(0, max(labels)+2 )
# plt.ylim(min(data_all_min) - 10, max(data_all_max)+2) ##这里可以设置一下x轴和y轴显示范围
y_major_locator = plt.MultipleLocator(2)
ax.yaxis.set_major_locator(y_major_locator) #设置y轴刻度为2
plt.xticks(labels,rotation=60) #设置x轴刻度及旋转
fontsize = 15
plt.xlabel('labelx)',fontsize=fontsize)
plt.ylabel('labely',fontsize=fontsize)
plt.title('title',fontsize=fontsize)
plt.show()