Sitemap
Vinay Kumar Verma

Vinay Kumar Verma

Sr Computer Vision Engineer | Using AI to Reduce if-else

Dynamic Graph Plotting — Matplotlib

3 min readMar 7, 2022

There are cases when we want to plot our data, and the data keeps on coming. We can't wait for the whole process to complete because it may take forever. But we also want to monitor the results. or show the results of what's happening. We will use matplotlib and data structure for it. Let's begin.

Get Vinay Kumar Verma’s stories in your inbox

Join Medium for free to get updates from this writer.

Thinking about the problem statement again, “we need to plot data constantly, showing the trend of our progress”. It has few words, one is the trend of progress. That is telling us we need past results along with new ones. so we need to store past values as well. The second word I see is Progress, meaning we don’t need all of the past data but need the latest results as well. That tells us we can delete the past data after having certain records, meaning a fixed-size window.

The final result would look like this, and it’s very simple and efficient to plot the graphs this way. We can use a common data structure Queue with fixed window size. Queue works in a FIFO manner, first in & first out. (the one came first, gets popcorn first and the last one gets popcorn last :P Movie Theater Analogy)

Steps to Create Dynamic Plot

  • Create a Queue of Fixed Length.
  • Generate the Points and save them to Queue.
  • Remove the first point.
  • Plot the Queue. Pause the Plot to visualize.
  • Clear the plot for the next set of values.

Adding and Removing to maintain the fixed size of queue can be easily done using python package, deque from collections. setting maxlen to the number of values you want to see at a time will present you with a Queue.

  1. Lets Plot points using plt.plot(queue) — Linear Graph will be created

2. Now, let's Scatter points using plt.scatter(range(len(queue)),queue) — Points will be scattered in the graph. Scatter needs X-Axis as input.

3. Combining both the Plots to get a mixed graph— Linear & Scattered will be created.

Finally, here is the code that you have understood clearly above (if not, give it a read its easy) I am using random data, but you got the sense why we are doing what? right!!

Conclusion, data structures are cool they are everywhere. Data presented visually is far far far better than numerical jargon. This method can be used in showing your classification model predictions on live video, the light variation, or even your CPU usage during the code execution. If you find this helpful or have doubts drop a comment I will reply, and follow for more simplified blogs.

Keep Coding, Keep Debugging, Bye Bye :)

--

--

Vinay Kumar Verma
Vinay Kumar Verma

Written by Vinay Kumar Verma

Sr Computer Vision Engineer | Using AI to Reduce if-else