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 fileds in apps_pd['when_start'] are int like 1499963856220, and same for how_long.
Result of the script
However, I got a really messy graph if I don't change the format of the data.
Is there anyway I could change the unit of x-axis to 'yyyy-mm-dd'? I want to group all my data in day level.
And also could I change the unit of y-axis to hour-min-second?
Here is the first 5 lines of my dataframe.
Behind the scenes, seaborn uses matplotlib to draw its plots. For interactive work, it's recommended to use a Jupyter/IPython interface in matplotlib mode, or else you'll have to call matplotlib. pyplot. show() when you want to see the plot.
Factor Plot is used to draw a different types of categorical plot . The default plot that is shown is a point plot, but we can plot other seaborn categorical plots by using of kind parameter, like box plots, violin plots, bar plots, or strip plots.
Seaborn provides an API on top of Matplotlib that offers sane choices for plot style and color defaults, defines simple high-level functions for common statistical plot types, and integrates with the functionality provided by Pandas DataFrame s.
First of all when you post data post it in text format not image.
You can convert col when_start
to date time format as follow:
apps_pd['when_start'] = pd.to_datetime(apps_pd['when_start'], unit='ms')
However scatter plot which is one of calls of lmplot does not support datetime format. You have to replace your xtick marks 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With