概述
本人是python新手,对python不熟,所以写了这篇博客用来方便自己
首先是csv文件,csv文件的生成是用java的,因为做实验的环境在java
CSV:
time,cost
1745.0093968037922,0.44017038833986216
1744.1923619737045,0.44020930380282514
1743.5844056242627,0.45103601130745474
1742.328138957596,0.45312215270421813
1552.3770421538036,0.4832019464694919
1551.4868421538035,0.485507344728751
1549.9001333333335,0.505857706356066
1549.3255666666669,0.506082564004214
java里面用来生成csv的代码
public class CSVUtil {
public static void saveCsv(List<? extends AbIndividual> abIndividuals,String fileName,boolean isNormalizing,boolean append){
try{
FileWriter fw = new FileWriter(fileName,append);
PrintWriter printWriter = new PrintWriter(new BufferedWriter(fw));
printWriter.print("time");
printWriter.print(",");
printWriter.print("cost");
printWriter.println();
double [][]data = new double[abIndividuals.size()][2];
double f1Min = Double.MAX_VALUE;
double f2Min = Double.MAX_VALUE;
double f1Max = Double.MIN_VALUE;
double f2Max = Double.MIN_VALUE;
for (int i = 0; i < abIndividuals.size(); i++) {
data[i][0] = abIndividuals.get(i).getFitness()[0];
f1Min = Math.min(f1Min,data[i][0]);
f1Max = Math.max(f1Max,data[i][0]);
data[i][1] = abIndividuals.get(i).getFitness()[1];
f2Min = Math.min(f2Min,data[i][1]);
f2Max = Math.max(f2Max,data[i][1]);
}
if(isNormalizing){
for (int i = 0; i < abIndividuals.size(); i++) {
data[i][0] = (data[i][0] - f1Min)/(f1Max - f1Min);
data[i][1] = (data[i][1] - f2Min)/(f2Max - f2Min);
}
}
for (int i = 0; i < abIndividuals.size(); i++) {
printWriter.print(data[i][0]);
printWriter.print(",");
printWriter.print(data[i][1]);
printWriter.println();
}
printWriter.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
import time
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
#坐标轴负号的处理
plt.rcParams['axes.unicode_minus']=False
# 读取数据
i = 1
while i < 100:
fws = 'D://demo//data/FWA'+str(i)+'.csv' #文件的路径
nagas = 'D://demo//data/NSGA'+str(i)+'.csv'
fwc = pd.read_csv(fws) #应该是读取这个文件,并且加载到数组里?
nagac = pd.read_csv(nagas)
# 绘制单条折线图
plt.scatter(fwc.time, # x轴数据
fwc.cost, # y轴数据
s=100,
marker='*',
c='g'
)
plt.scatter(nagac.time, # x轴数据
nagac.cost, # y轴数据
s=100,#应该是大小
marker='*',#标记符
c='r'#颜色
)
#对于X轴,只显示x中各个数对应的刻度值
plt.xticks(fontsize=8, ) #改变x轴文字值的文字大小
# 添加y轴标签
plt.ylabel('花费')
plt.xlabel('时间')
# 添加图形标题
plt.title('第'+str(i)+'代')
# 显示图形
plt.show()
i += 1
最后做出来的图
最后
以上就是寂寞冬天为你收集整理的读取csv并且绘制散点图的全部内容,希望文章能够帮你解决读取csv并且绘制散点图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复