Categories
Python Answers

How to dynamically update plot in Python matplotlib?

Sometimes, we want to dynamically update plot in Python matplotlib.

In this article, we’ll look at how to dynamically update plot in Python matplotlib.

How to dynamically update plot in Python matplotlib?

To dynamically update plot in Python matplotlib, we can call draw after we updated the plot data.

For instance, we write

import matplotlib.pyplot as plt
import numpy

hl, = plt.plot([], [])

def update_line(hl, new_data):
    hl.set_xdata(numpy.append(hl.get_xdata(), new_data))
    hl.set_ydata(numpy.append(hl.get_ydata(), new_data))
    plt.draw()

to define the update_line function.

In it, we call set_xdata to set the data form the x-axis.

And we call set_ydata to do the same for the y-axis.

Then we call plt.draw to redraw the plot with the new data.

Conclusion

To dynamically update plot in Python matplotlib, we can call draw after we updated the plot data.

Categories
Python Answers

How to add else clause on Python while statement?

Sometimes, we want to add else clause on Python while statement

In this article, we’ll look at how to add else clause on Python while statement.

How to add else clause on Python while statement?

To add else clause on Python while statement, we can add it below the while loop.

For instance, we write

while condition:
    handle_true()
else:
    handle_false()

to add a while loop that run while condition is True.

The else block runs when condition becomes False.

Conclusion

To add else clause on Python while statement, we can add it below the while loop.

Categories
Python Answers

How to split string on whitespace in Python?

Sometimes, we want to split string on whitespace in Python.

In this article, we’ll look at how to split string on whitespace in Python.

How to split string on whitespace in Python?

To split string on whitespace in Python, we can use the string split method.

For instance, we write

l =  "many   fancy word \nhello    \thi".split()

to call split on the "many fancy word \nhello \thi" string to return a list of the strings that’s split on whitespace.

Conclusion

To split string on whitespace in Python, we can use the string split method.

Categories
Python Answers

How to run certain code every n seconds with Python?

Sometimes, we want to run certain code every n seconds with Python.

In this article, we’ll look at how to run certain code every n seconds with Python.

How to run certain code every n seconds with Python?

To run certain code every n seconds with Python, we can use the threading.Timer class.

For instance, we write

import threading

def printit():
  threading.Timer(5.0, printit).start()
  print("Hello, World")

printit()

to run the printit function after 5 seconds.

With threading.Timer(5.0, printit).start().

We use printit as the function to run when creating the Timer so it’ll run every 5 seconds.

Conclusion

To run certain code every n seconds with Python, we can use the threading.Timer class.

Categories
Python Answers

How to find string between two substrings with Python?

Sometimes, we want to find string between two substrings with Python.

In this article, we’ll look at how to find string between two substrings with Python.

How to find string between two substrings with Python?

To find string between two substrings with Python, we call the re.search method with a regex that matches strings between 2 substrings.

For instance, we write

import re

s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print(result.group(1))

to call re.search with a regex string to match the strings that are between asdf and 123jasd in the string s.

Then we call result.group to get the match.

Conclusion

To find string between two substrings with Python, we call the re.search method with a regex that matches strings between 2 substrings.