In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how python multithreading to achieve dynamic map drawing related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
I. background
In some cases, in the face of real-time updated data, we hope to be able to visualize it in a window, and can be updated in real time, so that we can observe the changes of the data, so as to carry out data analysis, such as: drawing audio waveforms, drawing dynamic curves, etc., the following introduces the use of matplotlib combined with multi-thread to draw dynamic diagrams, hoping to help friends in need.
Scenario encountered: recently I happen to be learning the genetic algorithm in artificial intelligence and using this algorithm to solve TSP. Friends who know about this algorithm know that this algorithm is to find the optimal solution with large fitness through continuous iteration. In order to understand the change of fitness in the iterative process, I hope to update the fitness in the iterative process in real time and visualize it (the amount of data is increasing).
Step 1. Use matplotlib to draw dynamic diagrams
Tool: matplotlib.animation
2. Create a thread to update the data
Threading
3. Code framework # Author: shallow Ruoqingfeng cyf# Date: 2020/12/11import threadingimport matplotlib.pyplot as pltimport matplotlib.animation as animationimport matplotlib.lines as lineimport numpy as npCHUNK = 2048 # initial data quantity data=np.random.normal (0Magne1 Magazine Chunk) # stores data for drawing images The data type can be defined for the list # canvas fig = plt.figure () ax = plt.subplot (111Magneol ylim5) line = line.Line2D ([], []) # draw a straight line # initialize the image def plot_init (): ax.add_line (line) return line, # must add a comma, otherwise it will report an error (TypeError: 'Line2D' object is not iterable) # update the image (animation will call this function constantly to refresh the image Realize the effect of dynamic graph) def plot_update (I): global data # data is a global variable data_copy = data.copy () # in order to prevent the acquired data from being updated when the image is drawn due to thread asynchronism, a copy of the data is copied here Otherwise, the data dimensions of x and y may not be equal when drawing the image. X_data=np.arange (0 data copy. Shape [0], 1) # x axis is automatically generated based on y axis data (can be modified as needed) ax.set_xlim (0 data copy. Shape [0]) # Abscissa range (range and scale of Abscissa can be updated according to data length) ax.set_title ("title") Fontsize=8) # set title line.set_xdata (x_data) # update the data of the line line.set_ydata (data_copy) # update the data of the line # headings (if there are multiple subgraphs It can be subtitled) plt.suptitle ('Suptitle',fontsize=8) # re-render subgraph ax.figure.canvas.draw () # you must add this line of code to update title and coordinates! Return line, # must add a comma, otherwise an error will be reported (TypeError: 'Line2D' object is not iterable) # draw a dynamic graph ani = animation.FuncAnimation (fig, # canvas plot_update, # image update init_func=plot_init # Image initialization frames=1, interval=30, # Image update interval blit=True) # data update function def dataUpdate_thead (): global data # to facilitate understanding of the code Here we generate normally distributed random data while True: # in order to facilitate testing, let the data update data=np.random.normal (0Magazine 1) # create a separate thread for the data update function. Execute ad_rdy_ev = threading.Event () ad_rdy_ev.set () # set thread to run t = threading.Thread (target=dataUpdate_thead, args= ()) # update data concurrently with the thread of image rendering. Parameter description: target is a function that the thread needs to execute. Args is the parameter passed to the function) t.daemon = Truet.start () # Thread executes plt.show () # display image (0SCONlGOUNK) # create a separate thread for the data update function, and execute ad_rdy_ev = threading.Event () ad_rdy_ev.set () # concurrently with the thread of image drawing. # set thread to run t = threading.Thread (target=dataUpdate_thead, args= ()) # update data Parameter description: target is the function that the thread needs to execute, and args is the parameter passed to the function) t.daemon = Truet.start () # Thread executes plt.show () # shows that the image above is all the contents of the article "how to draw dynamic graphs with python multithreads". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.