这个代码风格跟我之前的不太一样哈,是我一个学弟写的,写得很好,我转载出来分享记录一下,作复杂图像更适合用这种代码风格。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import font_manager
#读取实验数据
data = pd.ExcelFile(r'488result.xlsx')
#data = pd.read_execl(r'C:\Users\Administrator\Desktop\DENSGA VS NSGA.xlsx')
data= data.parse('MotorCAD验证方案(10个)')
#从操作系统中读取所需的字体
font = font_manager.FontProperties(fname ='C:\\Windows\\Fonts\\times.TTF',size = 15)
#设置画布
fig = plt.figure(figsize=(8,8),dpi =200,frameon=True)
plt.style.use('seaborn')
#画验证点
# lines=plt.plot(data['obj_SystemEfficiency(calculated)'],data['obj_TorquePerVolume(calculated)'],'o
lines=plt.plot(data['SystemEfficiency(MOP)'],data['TorquePerVolume(MOP)'],"-o")
# line=lines[0]
# line.set_color('#00FF20') #设置颜色
# line.set_linewidth(1) #设置线宽
# line.set_alpha(1) #设置透明度
# line.set_label("Validated Design")
#画帕累托前沿
Pareto=lines[0]
Pareto.set_color('#FF0000') #设置颜色
Pareto.set_linewidth(1) #设置线宽
Pareto.set_alpha(1) #设置透明度
Pareto.set_label("Pareto front")
#标注Design
# for a,b,c in zip(data['obj_SystemEfficiency(calculated)'],data['obj_TorquePerVolume(calculated)'],data['Design Numbers']):
# plt.text(a+0.004,b,c,fontsize=9)
number=data['Design Numbers']
num=np.array(number)
for i in range(len(num)):
number[i]=format(number[i],'3f') #保留几位小数
for a,b,c in zip(data['SystemEfficiency(MOP)'],data['TorquePerVolume(MOP)'],number*10000):
plt.text(a-0.004,b-0.4,c,fontsize=9)
#设置坐标轴的格式
plt.xticks(np.arange(-95.75,-95.40,0.05),fontproperties = font)
plt.yticks(np.arange(-49.5,-39,1),fontproperties = font)
plt.xlabel(r'SystemEfficiency',fontproperties = font)
plt.ylabel(r'TorquePerVolume', fontproperties = font)
leg= plt.legend(loc='best',prop = font)
leg.get_frame().set_alpha(0) #取消边框
plt.savefig('111',bbox_inches = 'tight',pad_inches = 0,dpi =350)
?
|