Categories
Python Answers

How to adjust padding with cutoff or overlapping labels with Python matplotlib?

Sometimes, we want to adjust padding with cutoff or overlapping labels with Python matplotlib.

In this article, we’ll look at how to adjust padding with cutoff or overlapping labels with Python matplotlib.

How to adjust padding with cutoff or overlapping labels with Python matplotlib?

To adjust padding with cutoff or overlapping labels with Python matplotlib, we call plt.savefig with the bbox_inches argument set to 'tight'.

For instance, we write

plt.savefig('myfile.png', bbox_inches="tight")

to call savefig to save the flots to myfile.png.

We set bbox_inches to 'tight' to reduce padding between the plots.

Conclusion

To adjust padding with cutoff or overlapping labels with Python matplotlib, we call plt.savefig with the bbox_inches argument set to 'tight'.

Categories
Python Answers

How to test that a Python function throws an exception?

Sometimes, we want to test that a Python function throws an exception.

In this article, we’ll look at how to test that a Python function throws an exception.

How to test that a Python function throws an exception?

To test that a Python function throws an exception, we can use the assertRaises method.

For instance, we write

import my_mod

#...

class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertRaises(SomeException, my_mod.my_func)

to create the MyTestCase test class.

In it, we define the test1 test method that calls assetRaises with the exception we want to check for and the function that throws the exception respectively.

Conclusion

To test that a Python function throws an exception, we can use the assertRaises method.

Categories
Python Answers

How to get the logical xor of two variables in Python?

Sometimes, we want to get the logical xor of two variables in Python.

In this article, we’ll look at how to get the logical xor of two variables in Python.

How to get the logical xor of two variables in Python?

To get the logical xor of two variables in Python, we can use the ^ operator.

For instance, we write

bool(a) ^ bool(b)

to return the xor result of 2 booleans with ^.

Conclusion

To get the logical xor of two variables in Python, we can use the ^ operator.

Categories
Python Answers

How to programmatically generate video or animated GIF in Python?

Sometimes, we want to programmatically generate video or animated GIF in Python.

In this article, we’ll look at how to programmatically generate video or animated GIF in Python.

How to programmatically generate video or animated GIF in Python?

To programmatically generate video or animated GIF in Python, we can use the imageio library.

To install it, we run

pip install imageio

Then we can use it by writing

import imageio
with imageio.get_writer('/path/to/movie.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)

to create an animated GIF and save it to /path/to/movie.gif with imageio.getwriter.

In the with block, we loop through the filenames list to add the images at filename into the animated GIF with

image = imageio.imread(filename)
writer.append_data(image)

We read the image with imageio.imread and call append_data to append the image as a frame in the animated GIF.

Conclusion

To programmatically generate video or animated GIF in Python, we can use the imageio library.

Categories
Python Answers

How to state in requirements.txt a direct GitHub source with Python?

Sometimes, we want to state in requirements.txt a direct GitHub source with Python.

In this article, we’ll look at how to state in requirements.txt a direct GitHub source with Python.

How to state in requirements.txt a direct GitHub source with Python?

To state in requirements.txt a direct GitHub source with Python, we can add the GitHub repo path into requirements.txt.

For instance, we can specify the commit hash by writing

package-one==1.9.4
git+https://github.com/path/to/package-two@41b95ec#egg=package-two
package-three==1.0.1

in requiements.txt.

We specify the GitHub path in the 2nd line.

Also, we can specify the branch with

git+https://github.com/path/to/package-two@master#egg=package-two

We can specify the release with

git+https://github.com/path/to/package-two@0.1#egg=package-two

And we can specify the release with

git+https://github.com/path/to/package-two@releases/tag/v3.7.1#egg=package-two

Conclusion

To state in requirements.txt a direct GitHub source with Python, we can add the GitHub repo path into requirements.txt.