While learning from an online course on visualising data, I came across this line of code.
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
Can someone please tell me what is the use of
pd.plotting.register_matplotlib_converters()
I referred to the official documentation, but a clear explanation is not given. Documentation
I found the following response by Bletham on GitHub awhile back. https://github.com/facebook/prophet/issues/999
"Thanks for raising the issue.
This change was made to avoid a FutureWarning that was introduced around pandas 0.24. If you use fbprophet 0.4 with pd 0.24.2, then when you call plot on a Prophet model, you get
/usr/lib64/python2.7/site-packages/pandas/plotting/_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.
To register the converters:
>>> from pandas.plotting import register_matplotlib_converters
>>> register_matplotlib_converters()
warnings.warn(msg, FutureWarning)
Basically what's happening is that when you import pandas, it registers with matplotlib a bunch of functions that tell matplotlib how to plot pandas types. But, it does more than just that - it actually overwrites some built-in matplotlib handlers with pandas handlers. In particular, matplotlib has built-in the ability to plot datetime objects. When pandas is imported, it overwrites matplotlib's built-in datetime plotting with pandas datetime plotting.
Inside m.plot, we first convert everything out of pandas types before passing them along to matplotlib, specifically because we do not want to use pandas plotting. Despite that, in fbprophet v0.4 pandas was still being used for plotting because it overrode the datetime plotting. Thus plotting raised the FutureWarning despite us not using pandas types in plotting.
As noted in the FutureWarning, pandas is backtracking on the behavior and in the Future will require you to register the converters in order to plot pandas types in matplotlib. We thus decided to deregister them, to get the future behavior now.
The unfortunate thing here, though, is that it does break the behavior described in your post that previously worked, without any message as to why it doesn't work. In the future as more people upgrade to pd 0.24.2 and start to see the FutureWarning they will get in the habit of registering the converters prior to trying to plot their pandas types, but until then it will be a rocky transition. I mostly blame pandas for implementing this bad behavior and then backtracking on it in a not-backwards-compatible way, but perhaps there is something we could do to handle the transition better."
I found this in the documentation:
This function modifies the global matplotlib.units.registry dictionary. Pandas adds custom converters for
np.datetime64
...
So I guess it makes sure that pandas
datatypes like pd.Timestamp
can be used in matplotlib
plots without having to cast them to another type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With