Help with plot added padding (was fine previously)

Hi,

I regularly have been generating charts which looked fine, but all of a sudden some padding has appeared on the left and right of the plot, does anyone know why, and how to remove?

I also noticed the dates seem to have squashed together and the legend box text looks squashed.

I have added images of before and after. I did not change any plot code.

import pandas as pd
import matplotlib.pyplot as plt
from datetime import date

import factories

ax = plt.gca()  # get plot current axis and pass to df.plot() call to reuse

for func in factories.LAZY_STRATEGY_LIST:

    data = # get data

    df = pd.DataFrame(data)
    df['dt'] = df.timestamp.astype('int')
    df['dt'] = pd.to_datetime(df.dt, unit='s')
    df.drop(columns=['timestamp'], inplace=True)
    df.set_index('dt', inplace=True)

    cumsum = df.cumsum()
    cumsum.rename(columns={'pnl': s.name}, inplace=True)

    cumsum.plot(ax=ax)

plt.axhline(y=0, color='#000000', linestyle='-', alpha=0.5)

plt.savefig(f'plots/pnl-ALL-latest.png')
plt.savefig(f'plots/pnl-ALL-{date.today()}.png')

Thanks.

What versions of Matplotlib and Pandas do you have in the two cases?

The one on the left looks like it is truncating some of you data and the one on the right has a title. Given that you are plotting through the methods on the dataframe I suspect that this is a change in Pandas, but there is a chance this is Matplotlib regression.

Can you replace # get_data with something that will generate data that will reproduce the problem (random is fine). Things are much easier to debug when we can copy-paste example to run them.

1 Like

Ok so after having to refactor and delve into some matplotlib I have fixed the problem by just removing the margin:

plt.margins(x=0)

I also had to fix the dates layout with:

ax.xaxis.set_major_locator(mdates.MonthLocator())
fig.autofmt_xdate()