Matplotlib -- UserWarning: Attempting to set identical left == right == 737342.0 results in singular transformations;

Viewed 7441

By Using Matplotlib i am trying to create a Line chart but i am facing below issue. Below is the code. Can someone help me with any suggestion

Head = ['Date','Count1','Count2','Count3']

df9 = pd.DataFrame(Data, columns=Head)

df9.set_index('Date',inplace=True)
fig,ax = plt.subplots(figsize=(15,10))

df9.plot(ax=ax)

ax.xaxis.set_major_locator(mdates.WeekdayLocator(SATURDAY))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))

plt.legend()
plt.xticks(fontsize= 15)
plt.yticks(fontsize= 15)
plt.savefig(Line.png)

i am getting below error

Error: Matplotlib UserWarning: Attempting to set identical left == right == 737342.0 results in singular transformations; automatically expanding (ax.set_xlim(left, right))

Sample Data:

01-10-2010, 100, 0 , 100

X Axis: I am trying to display date on base of date every saturdays Y Axis: all other 3 counts

Can some one please help me whats this issue and how can i fix this...

2 Answers

The issue is caused by the fact that somehow, pandas.DataFrame.plot explicitly sets the x- and y- limits of your plot to the limits of your data. This is normally fine, and no one notices. In fact, I had a lot of trouble finding references to your warning anywhere at all, much less the Pandas bug list.

The workaround is to set your own limits manually in your call to DataFrame.plot:

if len(df9) == 1:
    delta = pd.Timedelta(days=1)
    lims = [df9.index[0] - delta, df9.index[0] + delta]
else:
    lims = [None, None]
df9.plot(ax=ax, xlim=lims)

This issue can also arise in a more tricky situation, when you do NOT only have one point, but only one cat get on your plot : Typically, when only one point is >0 and your plot yscale is logarithmic.

One should always set limits on a log scale when there 0 values. Because, there is no way the program can decide on a good scale lower limit.

Related