Skip to content

[OHLC Chart] How to keep the xaxis discrete #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dimitar-petrov opened this issue Sep 13, 2017 · 1 comment
Closed

[OHLC Chart] How to keep the xaxis discrete #829

dimitar-petrov opened this issue Sep 13, 2017 · 1 comment

Comments

@dimitar-petrov
Copy link

Hello,

I am tryging to plot minute financial data using Ohlc or Candlestick methods.
The trouble I am facing is with properly setting the xaxis to be discrete.

DATA Format: Intraday financial data for each day between 09:30 and 16:00.

Q1: Is it possible to plot bars only for the records that are present in the dataframe. I do not want to see empty space for the period between 16:00 and 09:30 next day and during weekends.

import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
import io

data = io.StringIO("""
datetime,high,low,open,close,volume,openinterest
2010-01-04 15:54:00,27.768,27.7602,27.7615,27.7615,16470171,42653
2010-01-04 15:55:00,27.7719,27.7602,27.7615,27.7641,16512576,42405
2010-01-04 15:56:00,27.768,27.7602,27.7654,27.7602,16572681,60105
2010-01-04 15:57:00,27.7628,27.7447,27.7628,27.7473,16648723,76042
2010-01-04 15:58:00,27.7486,27.7291,27.7473,27.7356,16744056,95333
2010-01-04 15:59:00,27.7563,27.7343,27.7343,27.7512,16850738,106682
2010-01-04 16:00:00,27.7719,27.7473,27.7486,27.7499,17065022,214284
2010-01-05 09:31:00,27.8289,27.7416,27.7887,27.7525,737681,309205
2010-01-05 09:32:00,27.755,27.6812,27.7525,27.7019,945764,208083
2010-01-05 09:33:00,27.7473,27.6942,27.7045,27.7369,1064084,118320
2010-01-05 09:34:00,27.8302,27.739,27.746,27.7887,1249313,185229
2010-01-05 09:35:00,27.8198,27.7369,27.7874,27.7499,1431892,182579
2010-01-05 09:36:00,27.7576,27.7278,27.7499,27.7395,1535106,103214
2010-01-05 09:37:00,27.7421,27.6929,27.7408,27.7324,1690391,146382
2010-01-05 09:38:00,27.7343,27.6631,27.7337,27.6877,1866990,176599
2010-01-05 09:39:00,27.7162,27.6812,27.689,27.7162,1965615,98625
2010-01-05 09:40:00,27.7188,27.6942,27.716,27.698,2043283,77668
""")

df = pd.read_csv(data, dtype=object, index_col='datetime', parse_dates=True)
trace = go.Ohlc(x=df.index,
                open=df.open,
                high=df.high,
                low=df.low,
                close=df.close)
layout = go.Layout(
    title='OHLC Financial Data',
    xaxis=go.XAxis(
        title='Time'),
    yaxis=go.YAxis(
        title='Price'))

data = go.Data([trace])
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='/tmp/findata.html')

example

I tried setting xaxis['type'] to 'category' but I get the following result which is either broken or misinterpretted by me.

example

@cldougl
Copy link
Member

cldougl commented Sep 14, 2017

Hi there,
This issue will be fixed in our core graphing library, plotly.js. You can see: plotly/plotly.js#2004 for progress on this issue.
In the meantime you could use the ticktext attribute:

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
import io

data = io.StringIO("""
datetime,high,low,open,close,volume,openinterest
2010-01-04 15:54:00,27.768,27.7602,27.7615,27.7615,16470171,42653
2010-01-04 15:55:00,27.7719,27.7602,27.7615,27.7641,16512576,42405
2010-01-04 15:56:00,27.768,27.7602,27.7654,27.7602,16572681,60105
2010-01-04 15:57:00,27.7628,27.7447,27.7628,27.7473,16648723,76042
2010-01-04 15:58:00,27.7486,27.7291,27.7473,27.7356,16744056,95333
2010-01-04 15:59:00,27.7563,27.7343,27.7343,27.7512,16850738,106682
2010-01-04 16:00:00,27.7719,27.7473,27.7486,27.7499,17065022,214284
2010-01-05 09:31:00,27.8289,27.7416,27.7887,27.7525,737681,309205
2010-01-05 09:32:00,27.755,27.6812,27.7525,27.7019,945764,208083
2010-01-05 09:33:00,27.7473,27.6942,27.7045,27.7369,1064084,118320
2010-01-05 09:34:00,27.8302,27.739,27.746,27.7887,1249313,185229
2010-01-05 09:35:00,27.8198,27.7369,27.7874,27.7499,1431892,182579
2010-01-05 09:36:00,27.7576,27.7278,27.7499,27.7395,1535106,103214
2010-01-05 09:37:00,27.7421,27.6929,27.7408,27.7324,1690391,146382
2010-01-05 09:38:00,27.7343,27.6631,27.7337,27.6877,1866990,176599
2010-01-05 09:39:00,27.7162,27.6812,27.689,27.7162,1965615,98625
2010-01-05 09:40:00,27.7188,27.6942,27.716,27.698,2043283,77668
""")

df = pd.read_csv(data, dtype=object, index_col='datetime', parse_dates=True)
trace = go.Ohlc(open=df.open,
                high=df.high,
                low=df.low,
                close=df.close)
layout = go.Layout(
    title='OHLC Financial Data',
    xaxis=go.XAxis(
        title='Time',
        tickmode='array',
        tickvals=list(range(len(df.index))),
        ticktext=df.index),
    yaxis=go.YAxis(
        title='Price'))

data = go.Data([trace])
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='findata')

more information on tick text here: https://plot.ly/python/reference/#layout-xaxis-ticktext

closing in favour of plotly/plotly.js#2004

@cldougl cldougl closed this as completed Sep 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants