近来实验室的师姐要发论文,由于论文交稿时间临近,有一些杂活儿需要处理,作为实验室资历最浅的一批,我这个实习生也就责无旁贷地帮忙当个下手。今天师姐派了一个小活,具体要求是:

给一些训练模型的迭代次数,训练精度的数据,让我做成图表形式展示出来,一方面帮助检查模型训练时的不足,另一方面来看样本数目和预测精度之间的联系,数据具体格式如下:

Iteration 1500

label train  test   right  acc

12    143 24    24    1.0

160 92    16    15    0.9375

100 12    2      0      0.0

142 0      0      0      0.0

152 0      0      0      0.0

110 10    2      0      0.0

170 12    2      2      1.0

42    421 70    63    0.9

31    43    8      5      0.625

22    132 22    18    0.818181818182

60    51    9      8      0.888888888889

51    916 153 143 0.934640522876

131 82    14    11    0.785714285714

53    84    14    10    0.714285714286

70    9      2      2      1.0

21    531 89    89    1.0

120 1      1      1      1.0

11    454 76    71    0.934210526316

90    1      1      1      1.0

32    39    7      6      0.857142857143

41    151 25    14    0.56

132 0      0      0      0.0

151 43    7      6      0.857142857143

43    8      2      1      0.5

80    7      2      1      0.5

141 96    16    16    1.0

44    67    12    2      0.166666666667

right: 509          accuracy:0.883680555556

我的任务就是以label为自变量,绘制出它和train及acc之间的关系。

接到这个任务后,最直观的感受就是常规的洗数据,于是我先把这些数据放在txt文件中存储下来,由于每个数据之间的间隔大于一个空格,我想当然地写个正则匹配脚本将数据间的大空格转换为一个逗号(转换为逗号的目的是这样可以直接转换为CSV表格文件,然而在本次任务中貌似意义不大….)

#**********************Python 3.6.1***************************#
#*            将txt文本数据中的过长的空格更为一个逗号           *#
#*****************   Author LQ  ******************************#
#********************** 2018/4/4  ****************************##!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os #os模块与文本操作直接相关的模块
#*********下面三句代码作用不详,就是为了防止出现编码问题*********
import importlib
import sys
importlib.reload(sys)
#****************************************************
PATTERN = '\s+'#匹配出文本中的长空格
class Cleaner:#初始化def __init__(self):os.chdir('D:\\Learning\\Machine_Learning\\实习\\师姐论文实验') #改变工作目录到txt文件对应的目录self.content = open("acc-onlyRealImage-Iter2500.txt")def grab_content(self):line=self.content.readline()pre=re.compile(PATTERN)while line:    line_1=pre.sub(',',line) #将文本的长空格转换为逗号后,利于转成CSV格式,然后label按照升序排列self.Write_content(line_1)line = self.content.readline()  def Write_content(self,line_1):path='acc-onlyRealImage-Iter2500-after.txt'f=open(path,'a')f.write('\n'+line_1)def run(self):  self.grab_content()if __name__ == '__main__':cleaner = Cleaner()    cleaner.run()

数据清洗完成后,自然就是绘图了,逛了一些博客后,着手写个脚本,第一版是绘制出label和train及acc的双Y轴折线图,脚本较为简单,就是调用别人造的轮子,直接附上代码:

#**********************Python 3.6.1***************************#
#*                      绘制出双Y轴折线图                      *#
#*****************   Author LQ  ******************************#
#********************** 2018/4/4  ****************************##!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os #os模块与文本操作直接相关的模块
import matplotlib.pyplot as plt
import numpy as np
#*********下面三句代码作用不详,就是为了防止出现编码问题*********
import importlib
import sys
importlib.reload(sys)
#****************************************************
font2 = {'family' : 'Times New Roman',  'weight' : 'normal',  'size'   : 18,  }class Drawing:#初始化def __init__(self):os.chdir('D:\\Learning\\Machine_Learning\\实习\\师姐论文实验') #改变工作目录到指定文件目录self.content = open("acc-onlyRealImage-Iter2200-after.txt")self.content1 = open("acc-onlyRealImage-Iter2500-after.txt")def grab_content(self):lines=self.content.readlines()lines_1=self.content1.readlines()x_1 = [line.strip().split(',')[0] for line in lines ]#字段以逗号分隔,这里取得是第4列y_train_1=[line.strip().split(',')[1] for line in lines ]y_train_2=[line.strip().split(',')[1] for line in lines_1 ]y_acc_1=[line.strip().split(',')[4] for line in lines ]y_acc_2=[line.strip().split(',')[4] for line in lines_1 ]x = list(range(len(x_1)))y_acc=[]y_acc1=[]y_train=[]y_train1=[]for i in range(len(y_acc_1)):y_acc.append(float(y_acc_1[i]))y_acc1.append(float(y_acc_2[i]))y_train.append(int(y_train_1[i]))y_train1.append(int(y_train_2[i]))#plt.xticks(x, x_1,rotation=0)fig,left_axis=plt.subplots()  p1, =left_axis.plot(x, y_train,'ro-')right_axis = left_axis.twinx()  p2, =right_axis.plot(x, y_acc,'bo-')                                                                                                        plt.xticks(x, x_1,rotation=0) #设置x轴的显示形式#设置左坐标轴以及右坐标轴的范围、精度left_axis.set_ylim(0,1201)  left_axis.set_yticks(np.arange(0,1201,200))  right_axis.set_ylim(0,1.01)  right_axis.set_yticks(np.arange(0,1.01,0.20))  #设置坐标及标题的大小、颜色left_axis.set_title('RealAndSimulation-Iter6600',font2)left_axis.set_xlabel('Labels',font2)left_axis.set_ylabel('Number of training sets',font2,color='r')left_axis.tick_params(axis='y', colors='r') right_axis.set_ylabel('Accuracy',font2,color='b') right_axis.tick_params(axis='y', colors='b') plt.show()def run(self):  self.grab_content()if __name__ == '__main__':Drawing = Drawing()    Drawing.run()

绘制出的图形如上所示,其实看起来也还不错,不过师姐表示有点乱,建议做个柱形的看看,于是继续撸代码:

#**********************Python 3.6.1***************************#
#*                     绘制单Y轴双变量柱状图                   *#
#*****************   Author LQ  ******************************#
#********************** 2018/4/4  ****************************##!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os #os模块与文本操作直接相关的模块
import matplotlib.pyplot as plt
import numpy as np
#*********下面三句代码作用不详,就是为了防止出现编码问题*********
import importlib
import sys
importlib.reload(sys)
#****************************************************
font2 = {'family' : 'Times New Roman',   #设置字体'weight' : 'normal',  'size'   : 18,  }class Drawing:#初始化def __init__(self):os.chdir('D:\\Learning\\Machine_Learning\\实习\\师姐论文实验') #改变工作目录到指定文件的目录self.content = open("acc-onlyRealImage-Iter2200-after.txt")self.content1 = open("acc-onlyRealImage-Iter2500-after.txt")def autolabel(self,rects,y): #在柱状图上面添加 数值i=0for rect in rects:#读出列表存储的value值value=y[i] x_1 = rect.get_x() + rect.get_width()/2y_1 = rect.get_height()#x_1,y_1对应柱形的横、纵坐标i+=1plt.text(x_1, y_1, value, ha='center', va='bottom',fontdict={'size': 8}) #在fontdict中设置字体大小rect.set_edgecolor('white')def Pictures(self):lines=self.content.readlines()lines_1=self.content1.readlines()x_1 = [line.strip().split(',')[0] for line in lines ]#字段以逗号分隔,这里取得是第1列y_train_1=[line.strip().split(',')[1] for line in lines ]y_train_2=[line.strip().split(',')[1] for line in lines_1 ]y_acc_1=[line.strip().split(',')[4] for line in lines ]y_acc_2=[line.strip().split(',')[4] for line in lines_1 ]x = list(range(len(x_1)))y_acc=[]y_acc1=[]y_train=[]y_train1=[]for i in range(len(y_acc_1)):y_acc.append(float(y_acc_1[i]))y_acc1.append(float(y_acc_2[i]))y_train.append(int(y_train_1[i]))y_train1.append(int(y_train_2[i]))plt.xticks(x, x_1,rotation=0) #设置X轴坐标值为label值for i in range(len(x)): #调整柱状图的横坐标,使得打印出来的图形看起来更加舒服 x[i] = x[i] -0.2 a=plt.bar(x, y_train,width=0.4,label='iter2200',fc = 'b') #a=plt.bar(x, y_acc,width=0.4,label='iter2200',fc = 'b') for i in range(len(x)):  x[i] = x[i] + 0.4  b=plt.bar(x, y_train1, width=0.4, label='iter2500',fc = 'r')#b=plt.bar(x, y_acc1, width=0.4, label='iter2500',fc = 'r')plt.xlabel('Labels',font2)  #设置Y轴值的范围plt.ylim((0, 1000))#设置Y轴的刻度值plt.yticks(np.arange(0,1001, 200))#plt.ylim((0, 1.1))#plt.yticks(np.arange(0,1.1, 0.2)) #plt.ylabel('Accuracy',font2)plt.ylabel('Number of training sets',font2) #字体的格式在font2中有设置self.autolabel(a,y_train_1) #为柱形图打上数值标签self.autolabel(b,y_train_2)#self.autolabel(a,y_acc_1)#self.autolabel(b,y_acc_2)#plt.title("RealAndSimulation",font2)plt.title("OnlyRealImage",font2)plt.legend()plt.show()def run(self):  self.Pictures()if __name__ == '__main__':Draw = Drawing()    Draw.run()

呈现的效果如下,此处因为对于双柱形图通常采用同一Y轴坐标系,所以此处选择的是比对不同迭代次数:

此处为了方便实验结果的观测,在每个柱形上面均打印出了对应的数值,至此,这部分的任务ending,难度不是很大,不过需要自己耐心编写脚本,调试出好的结果~

python绘制双Y轴折线图以及单Y轴双变量柱状图相关推荐

  1. python3+matplotlib绘制双轴折线图(两种方法)

    Background 这里提供两种方法,一种是基于pandas,另一种是基于twinx. 1.先看最终效果图 pandas twinx 2.源码 import pandas as pd import ...

  2. python实现在一个画布绘制多张双y轴折线图,y轴数据大小不一样,怎么hua?

    问题的提出 寻找资料时,发现要么是将多张图绘制在同一画布之上,要么是一张图绘制多条曲线,还有就是绘制双Y轴曲线图,而本人想将四张双Y轴折线图呈现在同一个画布之上,也就是使得四个两两坐标尺度不同的图片绘 ...

  3. Python 数据可视化教程 绘制精美的双 Y 轴折线图

    在可视化作图的时候,有时候需要将几条曲线放在同一个图中,但这些曲线值的大小范围不同,需要的刻度不同.如果都用同一个 Y 轴刻度,值较小的曲线变化将不明显(如深度学习训练和测试的 Loss 变化).下面 ...

  4. origin做双Y轴折线图的具体步骤

    1.导入数据: file-->Open excel 选择自己的数据集 2.插入图表 :plot-->Line-->Line,这是会弹出一个对话框: 3.通过导入的数据进行建图, 选择 ...

  5. PyQt5_pyqtgraph单Y轴和双Y轴折线图

    目录 效果: 代码: 使用: 数据: 效果: 代码: import sys import pandas as pd from PyQt5 import QtCore,QtWidgets from Py ...

  6. python画双折线图-Python Pandas 时间序列双轴折线图

    时间序列pv-gmv双轴折线图 import numpy as np import pandas as pd import matplotlib.pyplot as plt n = 12 date_s ...

  7. python如何绘制曲线图_python pandas plot画折线图如何显示x轴的值?

    在使用python pandas Series plot画折线图时,不知道该如何显示x轴的值. 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  8. python时间序列折线图_Python Pandas 时间序列双轴折线图

    时间序列pv-gmv双轴折线图 import numpy as np import pandas as pd import matplotlib.pyplot as plt n = 12 date_s ...

  9. [转载] Python数据可视化库-Matplotlib——折线图绘制

    参考链接: Python Matplotlib数据可视化 plot折线图 # coding:utf-8 import pandas as pd import numpy as np from matp ...

最新文章

  1. strcpy与strncpy的区别
  2. 42、BGP常用命令及注意事项
  3. FreeMarker基础语法教程
  4. InnoDB的行锁和表锁
  5. 在SAP Hybris commerce Storefront里购物下单
  6. apache mysql 登陆_Apache站点,注册登陆功能的实现
  7. 质量属性效用树例子_数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇
  8. CSS3 Media Query 响应式媒体查询
  9. TS DataType
  10. GCN pytorch实现 笔记
  11. 关于mybatis的mapper和mapper.xml注入spring托管的方法 超详细
  12. [原创]数论个人模板
  13. 做测试开发半年涨薪20W入职名企大厂,这位90后凭什么?
  14. 博客园美化资源网站链接
  15. DDD中的Specification模式
  16. AttributeError: Can't pickle local object 'BaseDataset.img_transformer.locals.lambda'
  17. DWG文件打开速度太慢怎么办!
  18. 3D打印在学生教育的有哪些应用?
  19. 【从嵌入式视角学习香山处理器】四、Chisel语言基础
  20. 拆解USB电压电流表,并分析测量原理(转数码之家)测电流需串一小电阻到电路。测电压不用按照文中,可以直接让电压正进AD的输入端口测试

热门文章

  1. Java NIO学习系列三:Selector
  2. spring使用@Async注解异步处理
  3. Javascript日期时间总结
  4. 科研人员的办公室是怎样的?
  5. 学术写作利器——LaTeX入门笔记整理(不定期更新,附加使用心得)
  6. 编程之美-重建二叉树方法整理
  7. C++中的值传递、指针传递、引用传递
  8. httpd-2.4编译安装基本步骤
  9. ASP.NET中利用DataList实现图片无缝滚动
  10. Linux命令——cp