Black Lives Matter. Please consider donating to Black Girls Code today.

The X axis is not representing the selected dataset

I have a time series data set and I need to plot different kinds of graph from it. With the following command I am extracting the day of the year from my data set:

train[‘DayOfYear’] = train[‘Dates’].map(lambda x: x.strftime("%m-%d")),

And when I plotted it with iplot:

“iplot(daysofayear.iplot(asFigure=True,
kind=‘scatter’,
xTitle=‘Days of a year’,
yTitle=‘Total crime count’,
title=‘San Francisco crime count’))”

I get:

As you can see the X axis is showing the days of year starting from 2001, but my data is starting from 2003. At the same time the X axis should show days of the year (i.e., 01-01…). I have tired to remove the index and make a new df with out index and plot it with:

iplot({‘data’: [ Scatter(x=days[‘index’], y=days[‘TotalCount’] )]})

But this also give me the same wrong plot. On the other hand, When I plot the same dataset with subplots() I get the correct X axis

I should point out that when I use “dt.year” to get for instance the counts for year the x axis comes our correctly.

Please let me know if you have any idea how to fix this issue.

Hi there,

I think you need to make sure that you are extracting your times in the correct form/syntax. I believe you want your data to start at 2003, which means you would need to extract that information from your initial line of code so that you are getting datetimes in the form MM-DD-YYYY. A good reference is the Python Datetime Docs as they have some useful information pertaining to this.

Give that a try and see what you get. :slight_smile:

Hi Adam,

Thank you for your reply.

As you can see in my data set I don’t have any year attached to my index. And I need to make a plot with x-axis of days of a year (i.e., 01-01, 01-02, 01-03 and …). When I matplotlib.pyplot.subplots() I get the following image which is correct.

I believe there is a bug in plot.ly which is wen you don’t extract new variable with out using Panda’s “dt” function it can’t read the index in a correct format. I was hoping that someone from the Plot.ly could take a look at this issue and address this bug.

Please let me know if you have any other thoughts on this matter.

Thanks,