0

There are plethora of different solutions, mostly outdated, to address the addition of axis in matplotlib object, haven't got any of them working yet. I want to get date labels (either Months or Years) on the x axis.

How to add the date annotations on the axis in Python with Pandas and Matplotlib?

MWE

import pandas as pd
from matplotlib.pyplot import savefig

a=pd.read_csv("test.csv")
a.pivot(index='date', columns='group').plot()
savefig("plot3.png")

test.csv

date,group,myval
2018-07-01,test1,999
2018-07-01,test2,523
2018-07-02,test1,3305
2018-07-02,test2,1290
2018-07-03,test1,9399
2018-07-03,test2,1827
2018-07-04,test1,6982
2018-07-04,test2,2391
2018-07-05,test1,4100
2018-07-05,test2,2081
2018-07-06,test1,0268
2018-07-06,test2,4834
2018-07-07,test1,9571
2018-07-07,test2,6369
2018-07-08,test1,3943
2018-07-08,test2,6483

enter image description here

where the time annotations are missing.

1
  • 2
    Convert colum date to datetime by a=pd.read_csv("test.csv", parse_dates=[0]) Commented Sep 17, 2018 at 7:23

1 Answer 1

2

Comments have the following solutions, solutions tested to work.

I. Convert date column to datetime dtype a['date'] = pd.to_datetime(a['date']) and plot.

a=pd.read_csv("test.csv")
a['date'] = pd.to_datetime(a['date']) 
a.pivot(index='date', columns='group').plot()

where adjust the formatting as needed such as

a['date'] = pd.to_datetime(a['date'], format='%Y-%m-%d') 

enter image description here

II. Convert colum date to datetime by a = pd.read_csv("test.csv", parse_dates=[0])

a=pd.read_csv("test.csv", parse_dates=[0]) 
a.pivot(index='date', columns='group').plot()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.