How to construct int for datetime on x-axis using nautical tree?

I am trying to use seaborn to plot a graph

sns.lmplot(x="when_start", y="how_long",hue= 'state',
           data=apps_pd.loc[(apps_pd['user'] == 'xavi')],lowess=True);

      

Where apps_pd is a dataframe . And the files in apps_pd ['when_start'] are int like 1499963856220 and also for how_long .

Script result

However, I got a really messy graph if I don't change the data format.

Is there anyway I can change the unit of the x-axis to "yyyy-mm-dd"? I want to group all my data at a daily level.

And also can I change the unit of the y-axis to hour-min-second?

Here are the first 5 lines of my frame.

+3


source to share


1 answer


First of all, when you send data, place it in text format, not in an image.

You can convert col when_start

format to date format like below:

apps_pd['when_start'] = pd.to_datetime(apps_pd['when_start'], unit='ms')

      



However, the scatter plot, which is one of the lmplot calls , does not support the datetime format . You have to replace your xtick signs after plotting like in this example:

import pandas as pd
import seaborn as sns
import matplotlib.pylab as plt

cols = ['how_long', 'state', 'user', 'when_start']
data = [[62297, 'FINISHED', 'xavi', 1499963793923],
 [25761, 'FINISHED', 'xavi', 1499963446385],
 [20082, 'FINISHED', 'xavi', 1499963221203],
 [20508, 'FINISHED', 'xavi', 1499963156760],
 [580975, 'FINISHED', 'xavi', 1499962435293]]

apps_pd = pd.DataFrame(data, columns = cols)
# convert ms timestamps of dataframe to date time format by pandas
#apps_pd['when_start'] = pd.to_datetime(apps_pd['when_start'], unit='ms')
print (apps_pd)

sns.lmplot(x='when_start', y='how_long', hue= 'state',
 data=apps_pd[(apps_pd['user'] == 'xavi')],lowess=True)

# get current axis
ax = plt.gca()
# get current xtick labels
xticks = ax.get_xticks()
# convert all xtick labels to selected format from ms timestamp
ax.set_xticklabels([pd.to_datetime(tm, unit='ms').strftime('%Y-%m-%d\n %H:%M:%S') for tm in xticks],
 rotation=50)

plt.show()

      

enter image description here

+1


source







All Articles