You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: Add ohlc / candlestick plotting option for DataFrame #783
Yes please implement candlestick plotting for DataFrame. It is so nice to make instant plots of financial data from yahoo with pandas but candelstick charts for many, like me, really are a must to analyze the data.
In order to support financial data I send mpl an ordinal index, essential np.arange(len(df.index)) and draw the ticks with a Locator/Formatter. Doing this with DatetimeIndex actually speeds up plotting #1579 and gets rid of weekend gaps. However, a side effect of this is that you have to keep the previous data around for future plots on the same axes. For that I have a Figure class that handles translating any future plots into an ordinal index. I chose to keep all plotted data around for plot specific fillnas, but that's probably unnecessary.
I'd like to merge this stuff in but I'm not sure how compatible it is. trtools.charting is fairly heavy handed in assuming you're not touching matplotlib directly. Mixing ordinal and datetime index plots on the same axes wouldn't work.
The module itself is self-contained outside of a small column_grep utility, so it can be made to be cleanly import-able as an add-on.
@dalejung, have you had any luck implementing this?
I agree that importing a big library for something like a candlestick plot seems heavy handed, maybe there's a lighter weight way we can get this going.
@wesm, how do you think this should work syntactically?
Rolling this functionality into plot might be neat.
For example, if the DataFrame has a datetime index and columns "open", "high", "low", "close", and a DataFrame.plot() is called, would it make sense for the result to be a candlestick plot by default?
Would this be better as a kwarg for DataFrame.plot() or as a separate function (e.g. ohlc_plot)
@woodb Sort of. I split out the charting work into its own project https://github.com/dalejung/ts-charting. The heaviness was due to me keeping the plotted data around in a DataFrame. I was doing this to use the DataFrame.__setitem__'s auto-reindexing to keep the plots aligned. I'm a bit smarter now and keep only index around.
I think ts-charting should be straight-forward to merge into pandas, at least a portion of it. When I get the time I'll open a PR.
ts-charting converts the DatetimeIndex to ints and translates the labels back with a custom Formatter. I'm pretty sure this could be made pandas compatible by always checking the current ax for our TimestampFormatter and reindexing to the current x-axis. I suppose the Figure and Grapher classes would need their methods refactored into flat functions or hidden away onto the Formatter.
Converting to an int-index is better but it means that every plot to that ax will need the translation. It's convenient to call fig.hl_span("2012-01-02", "2012-01-05"), but the base matplotlib method would not work ax.axvspan("2012-01-02", "2012-01-05") unless I monkey patched it.
Actually, thinking about it more. I dunno if that's acceptable for inclusion into pandas. Part of the reason for the Figure abstraction is to create a namespace where all methods are aware of the Datetime->int translation. hm.
Activity
esthers commentedon Feb 17, 2013
Yes please implement candlestick plotting for DataFrame. It is so nice to make instant plots of financial data from yahoo with pandas but candelstick charts for many, like me, really are a must to analyze the data.
wesm commentedon Feb 17, 2013
I'd be happy to accept a pull request for this
dalejung commentedon Feb 19, 2013
I have this here:
http://nbviewer.ipython.org/4982660/
In order to support financial data I send mpl an ordinal index, essential np.arange(len(df.index)) and draw the ticks with a Locator/Formatter. Doing this with DatetimeIndex actually speeds up plotting #1579 and gets rid of weekend gaps. However, a side effect of this is that you have to keep the previous data around for future plots on the same axes. For that I have a Figure class that handles translating any future plots into an ordinal index. I chose to keep all plotted data around for plot specific fillnas, but that's probably unnecessary.
I'd like to merge this stuff in but I'm not sure how compatible it is. trtools.charting is fairly heavy handed in assuming you're not touching matplotlib directly. Mixing ordinal and datetime index plots on the same axes wouldn't work.
The module itself is self-contained outside of a small column_grep utility, so it can be made to be cleanly import-able as an add-on.
esthers commentedon Feb 20, 2013
@dalejung, if you could find a way to merge your code it would be greatly appreciated.
woodb commentedon Jul 9, 2013
@dalejung, have you had any luck implementing this?
I agree that importing a big library for something like a candlestick plot seems heavy handed, maybe there's a lighter weight way we can get this going.
woodb commentedon Jul 9, 2013
@wesm, how do you think this should work syntactically?
Rolling this functionality into
plotmight be neat.For example, if the
DataFramehas adatetimeindex and columns "open", "high", "low", "close", and aDataFrame.plot()is called, would it make sense for the result to be a candlestick plot by default?Would this be better as a
kwargforDataFrame.plot()or as a separate function (e.g.ohlc_plot)dalejung commentedon Jul 9, 2013
@woodb Sort of. I split out the charting work into its own project https://github.com/dalejung/ts-charting. The heaviness was due to me keeping the plotted data around in a
DataFrame. I was doing this to use theDataFrame.__setitem__'s auto-reindexing to keep the plots aligned. I'm a bit smarter now and keep only index around.I think
ts-chartingshould be straight-forward to merge intopandas, at least a portion of it. When I get the time I'll open a PR.woodb commentedon Jul 9, 2013
@dalejung Prima, let me know if you need any help, I've got some time here and there.
dalejung commentedon Jul 9, 2013
@woodb Actually, a smaller first pass could be done with http://nbviewer.ipython.org/5864433. That uses the
matplotlibdatetime handling.ts-chartingconverts theDatetimeIndexto ints and translates the labels back with a custom Formatter. I'm pretty sure this could be madepandascompatible by always checking the currentaxfor ourTimestampFormatterand reindexing to the current x-axis. I suppose theFigureandGrapherclasses would need their methods refactored into flat functions or hidden away onto the Formatter.Converting to an int-index is better but it means that every plot to that
axwill need the translation. It's convenient to callfig.hl_span("2012-01-02", "2012-01-05"), but the basematplotlibmethod would not workax.axvspan("2012-01-02", "2012-01-05")unless I monkey patched it.Actually, thinking about it more. I dunno if that's acceptable for inclusion into
pandas. Part of the reason for theFigureabstraction is to create a namespace where all methods are aware of the Datetime -> int translation. hm.Go Gators??!
phobson commentedon Dec 13, 2013
Thought y'all might be interesting in this:
matplotlib/matplotlib#2643
If merged, you'll be able to feed a matplotlib axes object a list of dictionaries "stats" describing the boxplots via a
bxpmethodphobson commentedon Dec 16, 2013
@y-p -- yeah. What I've heard is that
matplotlib.financewill be deprecated in mpl 1.4 and removed by 1.5.jreback commentedon Dec 16, 2013
@phobson new champion for a combined viz library? (built on pandas of course) :)
jtratner commentedon Dec 16, 2013
I was discussing this out of band with @olgabot. @mwaskom 's seaborn could be a good candidate (and it already appears to use pandas).
6 remaining items
wesm commentedon Sep 29, 2016
Won't fix
femtotrader commentedon Oct 12, 2016
Some basic code to display daily OHLCV data
Related SO questions:
jreback commentedon Oct 12, 2016
@femtotrader you are just plotting a box-plot.
http://pandas.pydata.org/pandas-docs/stable/visualization.html#box-plots
femtotrader commentedon Oct 12, 2016
An example of box-plot with such a DataFrame will be nice.
I tried
and get
It doesn't look very well