python3 读取txt文件数据,绘制趋势图,matplotlib模块
python3 读取txt文件数据,绘制趋势图
test1.txt内容如下:
1 2 3 4 5 6 | 时间 / min cpu使用率 / % 内存使用率 / % 01 / 12 - 17 : 06 0.01 7.61 01 / 12 - 17 : 07 0.03 7.61 01 / 12 - 17 : 08 0.3 7.61 01 / 12 - 17 : 09 0.7 7.61 01 / 12 - 17 : 10 0.1 7.61 |
脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import matplotlib.pyplot as plt from itertools import islice import os a = [] b = [] c = [] with open (r 'D:\result\test1.txt' ,mode = 'r' ,encoding = 'utf-8' ) as f: #以只读方式打开txt文本文件 for lines in islice(f, 1 , None ): #去掉首行,读取所有行 lines = lines.rstrip( "\n" ) #去掉读取出来的换行符 lines1 = lines.split( " " )[ 0 ] #以空格为分割,获取第1个值 lines2 = lines.split( " " )[ 1 ] #以空格为分割,获取第2个值 lines3 = lines.split( " " )[ 2 ] #以空格为分割,获取第2个值 a.append(lines1) #将值追加至a列表 b.append( float (lines2)) #将值追加至b列表,并将列表的str类型转换为float c.append( float (lines3)) #将值追加至c列表,并将列表的str类型转换为float ax1 = plt.subplot( 211 ) # 创建子图 ax1 ax2 = plt.subplot( 212 ) # 创建子图 ax2 plt.sca(ax1) # 选择子图ax1 plt.tight_layout() # 设置子图之间默认的间距 #设置x,y轴 plt.plot(a,b) #设置x轴显示间隔(假如x轴数据较多,x轴显示重叠,我们可以控制x轴的显示间隔) plt.xticks( range ( 0 , 4 , 2 )) #给图标指定标题 plt.title( "CPU" ,fontsize = 24 ) #为X轴设置标题 plt.xlabel( "Time/m" ,fontsize = 14 ) #为Y轴设置标题 plt.ylabel( "CPU/%" ,fontsize = 14 ) #设置刻度标记大小,rotation表示刻度值倾斜角度 plt.xticks(a,rotation = 60 ,color = 'blue' ) plt.sca(ax2) # 选择子图ax2 plt.plot(a, c) # 在子图ax2 中绘制 #展示所有图表 plt.show() |
效果图如下:
绘图参考:
https://blog.csdn.net/u014453898/article/details/73395522
https://www.cnblogs.com/zhizhan/p/5615947.html
新增鼠标移动标注
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import matplotlib.pyplot as plt import matplotlib.ticker as ticker import os from itertools import islice from matplotlib import font_manager as fm, rcParams plt.rcParams[ 'font.sans-serif' ] = [ 'SimHei' ] #显示中文标签 plt.rcParams[ 'axes.unicode_minus' ] = False #这两行需要手动设置 a = [] b = [] # fig = plt.figure() # po_annotation = [] with open (r 'D:\result\biaogancloud.txt' ,mode = 'r' ,encoding = 'utf-8' ) as f: for lines in islice(f, 1 , None ): lines = lines.rstrip( "\n" ) # print(lines.split(" ")[1]) lines1 = lines.split( " " )[ 0 ] lines2 = lines.split( " " )[ 1 ] a.append(lines1) b.append( float (lines2)) # ---------- 画图 ---------- fig, ax = plt.subplots() # 网格显示 ax.grid(color = 'b' , linewidth = '0.3' ,linestyle = '-.' ) #折线图 ax.plot(a, b, color = 'royalblue' , lw = 2.5 , label = 'data' ) # 折线图上的散点 ax.scatter(a, b, marker = 'o' , c = 'darkgreen' ) ax.scatter(a, b, marker = 'o' , c = 'firebrick' ) # 一些小设置 # 设置 x 轴显示密度 # tick_spacing = 8 # ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) # 设置 x 坐标轴标签的显示内容和大小 plt.xlabel( '时间' , fontsize = 16 ) # 设置 x 坐标轴刻度的旋转方向和大小 plt.xticks(rotation = 90 , fontsize = 16 ) # 设置 y 坐标轴刻度大小 plt.yticks(fontsize = 18 ) # 坐标轴中文显示 plt.rcParams[ 'font.sans-serif' ] = [ 'SimHei' ] # 调整图的位置 plt.subplots_adjust(top = 0.9 , bottom = 0.22 ) # 设置尺寸 fig.set_size_inches( 24 , 12 ) po_annotation1 = [] for i in range ( len (a)): # 标注点的坐标 point_x = a[i] point_y = b[i] point, = plt.plot(point_x, point_y, 'o' , c = 'darkgreen' ) # 标注框偏移量 offset1 = 10 offset2 = 10 # 标注框 bbox1 = dict (boxstyle = "round" , fc = 'lightgreen' , alpha = 0.6 ) # 标注箭头 arrowprops1 = dict (arrowstyle = "->" , connectionstyle = "arc3,rad=0." ) # 标注 annotation = plt.annotate(text = (a[i],b[i]), xy = (a[i], b[i]), xytext = ( - offset1, offset2), textcoords = 'offset points' , bbox = bbox1, arrowprops = arrowprops1, size = 15 ) # 默认鼠标未指向时不显示标注信息 annotation.set_visible( False ) po_annotation1.append([point, annotation]) # 定义鼠标响应函数 def on_move(event): visibility_changed = False for point, annotation in po_annotation1: should_be_visible = (point.contains(event)[ 0 ] = = True ) if should_be_visible ! = annotation.get_visible(): visibility_changed = True annotation.set_visible(should_be_visible) if visibility_changed: plt.draw() # 鼠标移动事件 on_move_id = fig.canvas.mpl_connect( 'motion_notify_event' , on_move) plt.show() |
本文作者:香菜哥哥
本文链接:https://www.cnblogs.com/yizhipanghu/p/14271311.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步