I find the pandas DataFrame.plot method very useful and I especially like the way it formats the x ticks when the x variable is a date. However, there are times when I want to add more complicated marks on top of a plot that was generated using DataFrame.plot. For example, plotting a time series using ax = my_timeseries_df.plot("date", "value) and then calling the fill_between method on the ax object to add confidence intervals. However, when I call a plotting method on an ax object after using it with pandas I can't get any marks to show up.
Here is an example:
And here is the raw code:
import matplotlib.pyplot as plt
import pandas as pd
test = pd.DataFrame({
"a": pd.date_range(start="2020-04-21", periods=5, freq="D"),
"b": [1, 2, 3, 4, 5],
"c": [2, 4, 6, 8, 10],
})
fig, ax = plt.subplots()
test.plot("a", "b", ax=ax)
ax.plot(test.a, test.c, color="red", linewidth=20)
Strangely the y-limit changes after calling ax.plot(...) to accommodate the new points, but the new line does not appear.
I have tried adding %matplotlib inline to the cell but that doesn't help. I could always just bypass DataFrame.plot and use matplotlib for everything, but dealing with time-based x-ticks directly in matplotlib is annoying enough that I would love to let pandas do it for me.



ax.set_xlim(auto=True)to figure out if the line is plotted on a different part of the x-axis. This can happen when using different modules (Pandas and Matplotlib) that handle the dates differently.ax.set_xlim(auto=True)unfortunately.