1
votes

I would like to plot a graph with the most recent dates on the left instead of the right of the x-axis.

Is there a way to do this in pandas and matplotlib and still get the date axis?

Invert an axis in a matplotlib grafic

shows how to do this for the y-axis using invert_yaxis(). However, this is not available for xaxis.

1

1 Answers

1
votes

Set xlim() from pyplot. Let's take this example:

period = pd.period_range("1.1.2013","12.1.2013",freq="M")
data = np.arange(12)
s = pd.Series(data=data,index=period)
#Output
2013-01     0
2013-02     1
2013-03     2
2013-04     3
2013-05     4
2013-06     5
2013-07     6
2013-08     7
2013-09     8
2013-10     9
2013-11    10
2013-12    11

Set first value of xlim to be last index of series and second value to be the first index, like this:

s.plot()
plt.xlim(s.index[-1],s.index[0])
plt.show()