Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Ignore weekend interpolation for financial time-series python

Hi,

I seem to run into the same problem using plotly as with matplotlib whenever the time-series does not have data for the weekend, the plot simply interpolates between the dates. Is there any workaround to ignore missing time interpolation?

The matplotlib workaround is here: http://matplotlib.org/examples/pylab_examples/date_index_formatter.html

I would appreciate it if someone has found out a method with plotly.

Thanks.

Hi,

A simple way to work around this is to just put None as the value in your y list (the array or list you are using for your y axis values) and if you have a steady incrementing list of dates as your x, it will skip over these.

Here’s an example:

# weekends appear on days 8 and 9 of October 2016

#x = ['10-06-2016', '10-07-2016', '10-08-2016', '10-09-2016', '10-10-2016', '10-11-2016', '10-12-2016']
x = ['10-06-2016', '10-07-2016', '10-08-2016', '10-09-2016', '10-10-2016', '10-11-2016', '10-12-2016']
y = [0,1,None,None,2,3,4] 

trace = Scatter(x=x,
                y=y,
                mode='markers')

data = [trace]
py.iplot(data)

Hey Adam, I tried your method and it removed the linear interpolation on weekends but I now have gaps where weekends are. Is there a way to remove the gap? By the way I’m using mode=‘lines’ not markers so the resulting chart gap looks a lot more pronounced.

Yeah, all you have to do is just remove the weekend dates from your list, as well as the None values in the y list. I just tried it and it works well with the lines mode.

Hey Adam,

The thing is, I have half hour increments in the data and I want to skip over the entire weekends (say 96 data points). I tried just about everything. I even was able to generate lists of x and y where it shows None on the weekends for both variables. But mind you I have half hour increment. So I dont think it satisfies your steady increment requirement unless you meant something else.

It seems that whatever I do, even if I change the x variable object type to strings, plotly still thinks my x variable is time-series and creates gaps in the graph.

I need a workaround for this…

Thanks.

Hey Adam,

I just tried your example in the first post in python and it actually creates gap. So I’m not using ipython if that is why. I’m using the py.plot for plotting. Please let me know why this is.

Thanks.

hi
try type=“category” in x axis

2 Likes

Sorry for deterring the topic but just to say I had the same problem and tried your solution (chaitu2chowdary) and it solved the issue, Thank you very much !!

Can someone share specifically how/where to set the xaxis attribute?

I’m unclear whether it should be set in the call to go.Candlestick(…) or if it needs to be specified in a Layout parameter. I’ve tried both but receive parsing errors for each.

I’m sure it’s something simple…

Hi @spm,
The xaxis property goes under layout. So here you’d want something like

fig.layout.xaxis.type = 'category'

or

trace = go.Scatter(x=x,
                   y=y,
                   mode='markers')

layout = go.Layout(xaxis={'type': 'category'})
fig = go.Figure(data=[trace], layout=layout)
py.iplot(fig)

Hope that helps!
-Jon

1 Like

Hi @jmmease

It helps with main issue but now i am getting congested x-axis.

is it possible to have a clear x-axis if we are selecting time frame of more than a year?

Hi @rajputankur56, welcome to the forums!

Could you start a new thread with a full reproducible example, along with a description of what you’d like to change? That will make it a lot easier to work through it with you. Thanks!

-Jon

Hi, if I write fig.layout.xaxis.type = ‘category’, I delete weekend holes in a candlestick chart. However, if you plot another line below the graph (such as an technical indicator or an oscillator) the x-axes no longer move together … this is a major problem !!! where am I wrong? Many thanks guys