In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Python how to draw the line chart of the change of computer CPU share, which is introduced in great detail and has certain reference value. Interested friends must finish reading it!
Step 1: get the CPU share information of the system
The psutil module is a module dedicated to obtaining system information. In this module, a cpu_percent () method is used to obtain the CPU share of the system. We get the CPU share every 1 second, and store the information in the specified file and save it. In this case, I named the file cpu.txt
The code for obtaining the information in the first step is as follows:
# the module import psutilimport time # m that acquires system information draws the share of CPU every other second; how to persist it? How to match the time with the corresponding cpu share; while True: # get the current time and the cpu share t = time.localtime () cur_time ='% DV% dV% d'% (t.tm_hour, t.tm_min, t.tm_sec) cpu_res = psutil.cpu_percent () # print (cpu_res) # save to the file With open ('cpu.txt',' asides') as f: f.write ('% s% s\ n'% (cur_time, cpu_res)) time.sleep (1)
After running, you can open the file, and you can see the CPU share of the storage at the corresponding time:
Step 2: draw the obtained information into a line chart
Pyecharts module is used to draw graphics module, in which Line is to draw line chart, if you want to draw scatter plot, import Scatter. We are going to draw a line chart, so we are importing Line.
First create two empty lists, one to store time changes, one to store changes in CPU share, and then open the previously saved information, and traverse each line of the file, add the content to the list, and then draw the contents of the list on OK.
The code is as follows:
Mport randomfrom pyecharts import options as optsfrom pyecharts.charts import Line # get the data information that the line chart needs to be drawn X = [] y = [] with open ('cpu.txt') as f: # Open the file for line in f: # iterate through each line of the file in turn time, per = line.split () # return the cpu share of the time and the corresponding time x.append (time) y.append (per) # instantiate the line class to the line object And add points corresponding to x and y Line = (Line () .add _ xaxis (x) .add _ yaxis ("", y) .set _ global_opts (title_opts=opts.TitleOpts (title= "Cpu share scatter plot")) # saves the line chart information to a file; line.render ()
After running, you can get a render.html file. Open it and select the browser you want to open in the upper right corner, and the drawing will be displayed, as shown below:
The above is all the contents of the article "how to draw a line chart of computer CPU share changes by Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.