0

I recently come across plotly which is a very powerful interactive online plot system. So i was thinking converting plots generated by matplot into plotly. Everything works fine except for the time series plot.

here is a simple version of my code.

import pandas as pd
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.tools as tls
tls.set_credentials_file(
        username="edwinwang1988", 
        api_key="o8xw6s61dn")
import numpy as np

x = pd.date_range('1/1/2001',periods =72,freq='D')  # genrate a date range
Series = pd.DataFrame(x,columns=['Date']) # generate a date range data frame

np.random.seed(0)
col = ['A','B','C','D'] #set col names for return series
s = pd.DataFrame(np.random.randn(72,4),columns=col) # generate random return series with col names as col
for i in range(len(col)):
    s[col[i]] = s[col[i]].cumsum()

s['Date']=x  # add date to dataframe s
s.plot(x='Date') # plot s and set date as x axis

this works fine and give me the plot i want with date as x axis. but when i tried to convert the figure to plotly.

mpl_fig2= plt.gcf()
py.iplot_mpl(mpl_fig2,strip_styple = True,filename='test')

i am seeing code error like "min() arg is an empty sequence" and sometimes this "year=1 is before 1900; the datetime strftime() methods require year >= 1900"

1 Answer 1

1

Full disclosure, I work for Plotly.

tl;dr:

run this in your terminal and it should be fixed:

pip install --upgrade plotly

Context:

So, pandas can do some funny things to dates in matplotlib. This is how matplotlib handles dates, basically:

day 1 of year 1 in utc is the floating point 1.0

http://matplotlib.org/api/dates_api.html#module-matplotlib.dates

Depending on how you use pandas to format your dates, you can end up with a pandas TimeSeries_DateFormatter. To the best of my knowledge, the repercussion of this is actually to change the underlying data associated with the date times to the following convention:

day 1 of year 1970 in utc is the floating point 0.0

So, days (not seconds!) since the epoch (http://en.wikipedia.org/wiki/Unix_time). Fyi, this goes back to ~9-22-1677 by using negative numbers.

Issue:

plotly was treating datetimes inside matplotlib as matplotlib datetimes, but pandas adds a formatter that changes this. A patch is added in plotly 1.4.2

Sign up to request clarification or add additional context in comments.

1 Comment

still doesn't work for some reason. I am getting the same error code. @theengineear

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.