Categories
Python Answers

How to plot time in Python with Matplotlib?

Sometimes, we want to plot time in Python with Matplotlib.

In this article, we’ll look at how to plot time in Python with Matplotlib.

How to plot time in Python with Matplotlib?

To plot time in Python with Matplotlib, we can use datetime objects.

For instance, we write

import matplotlib.pyplot
import matplotlib.dates
from datetime import datetime

x_values = [datetime(2021, 11, 18, 12), datetime(2021, 11, 18, 14), datetime(2021, 11, 18, 16)]
y_values = [1.0, 3.0, 2.0]

dates = matplotlib.dates.date2num(x_values)
matplotlib.pyplot.plot_date(dates, y_values)

to add datetime objects into the x_values list.

Then we convert the datetimes to something that we can plot with

dates = matplotlib.dates.date2num(x_values)

And then we write

matplotlib.pyplot.plot_date(dates, y_values)

to plot the values in dates as the values in the x-axis.

Conclusion

To plot time in Python with Matplotlib, we can use datetime objects.

Categories
Python Answers

How to use property() on classmethods with Python?

Sometimes, we want to use property() on classmethods with Python.

In this article, we’ll look at how to use property() on classmethods with Python.

How to use property() on classmethods with Python?

To use property() on classmethods with Python, we can just use them together.

For instance, we write

class G:
    @classmethod
    @property
    def __doc__(cls):
        return f'A doc for {cls.__name__!r}'

to call both the classmethod and property decorators on the __doc__ method in class G.

Then we can access the cls class in it.

This works since Python 3.9.

The order of the decorators has to be the same as listed.

Conclusion

To use property() on classmethods with Python, we can just use them together.

Categories
Python Answers

How to add Python Pandas data to an existing csv file?

Sometimes, we want to add Python Pandas data to an existing csv file.

In this article, we’ll look at how to add Python Pandas data to an existing csv file.

How to add Python Pandas data to an existing csv file?

To add Python Pandas data to an existing csv file, we can use the to_csv method.

For instance, we write

df.to_csv('my_csv.csv', mode='a', header=False)

to call to_csv with the csv file name, mode set to 'a' for append, and header set to False to avoid append the header to the file.

Conclusion

To add Python Pandas data to an existing csv file, we can use the to_csv method.

Categories
Python Answers

How to override a dict with Python?

Sometimes, we want to override a dict with Python.

In this article, we’ll look at how to override a dict with Python.

How to override a dict with Python?

To override a dict with Python, we can create a subclass of the MutableMapping class.

For instance, we weite

from collections.abc import MutableMapping


class TransformedDict(MutableMapping):
    def __init__(self, *args, **kwargs):
        self.store = dict()
        self.update(dict(*args, **kwargs))  

    def __getitem__(self, key):
        return self.store[self._keytransform(key)]

    def __setitem__(self, key, value):
        self.store[self._keytransform(key)] = value

    def __delitem__(self, key):
        del self.store[self._keytransform(key)]

    def __iter__(self):
        return iter(self.store)
    
    def __len__(self):
        return len(self.store)

    def _keytransform(self, key):
        return key

to create a TransformedDict class that is a subclass of the MutableMapping.

We use a dict as the value of the store instance variable.

And then we manipulate it in the instance methods to create our own behavior for a TransformedDict dict.

The __getitem__ method is used to get the item from the store dict.

__setitem__ sets the item in the store.

__delitem__ deletes an itemn with the key from the store dict.

__iter__ returns an iterator from the store dict.

__len__ returns the length of the dict.

And _keytransform transforms the dict key to our liking.

Conclusion

To override a dict with Python, we can create a subclass of the MutableMapping class.

Categories
Python Answers

How to create a zip archive of a directory with Python?

Sometimes, we want to create a zip archive of a directory with Python.

In this article, we’ll look at how to create a zip archive of a directory with Python.

How to create a zip archive of a directory with Python?

To create a zip archive of a directory with Python, we can use the shutil.make_archive method.

For instance, we write

import shutil

shutil.make_archive(output_filename, 'zip', dir_name)

to call shutil.make_archive with the output_filename of the zip archive, 'zip', and the dir_name of the folder to zip.

Conclusion

To create a zip archive of a directory with Python, we can use the shutil.make_archive method.