surya rahul
surya rahul

Reputation: 883

How to show label as it is in the data?

I am plotting a graph between time and value. I want these time label to be shown as 2018-06-30 18:35:45 as in the main csv data.

Instead the graph on x axis shows time as 06:30 20.

How can the labels of x axis can be exact as mentioned in the main data.

Code I used is:

 import pandas as pd
 import matplotlib.pyplot as plt 

 df = pd.read_csv('Mlogi_ALL_idle_day.csv')

 df['time']=pd.to_datetime(df['time'], unit='ns')

 x = df['time']
 y = df['d']

 fig, ax = plt.subplots()
 ax.plot_date(x, y, linestyle='-')
 plt.title('Idle for 1 July - 2 July 2018')
 plt.legend()
 plt.ylabel('duration')
 plt.xlabel('Time')
 fig.autofmt_xdate()
 plt.show()

And the data look like:

 In[10]:df.head()
 Out[10]: 
             time            d
 0 2018-06-30 18:35:45  41000000000
 1 2018-06-30 18:36:47  44000000000
 2 2018-06-30 18:37:46  43000000000
 3 2018-06-30 18:38:46  40000000000
 4 2018-06-30 18:39:47  43000000000

Upvotes: 0

Views: 38

Answers (1)

Hiho
Hiho

Reputation: 663

To change the date label of the major x-axis tick you can use

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))

Upvotes: 1

Related Questions