-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
ENH: Add ohlc / candlestick plotting option for DataFrame #783
Comments
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. |
I'd be happy to accept a pull request for this |
I have this here: 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, if you could find a way to merge your code it would be greatly appreciated. |
@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 For example, if the Would this be better as a |
@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 I think |
@dalejung Prima, let me know if you need any help, I've got some time here and there. |
@woodb Actually, a smaller first pass could be done with http://nbviewer.ipython.org/5864433. That uses the
Converting to an int-index is better but it means that every plot to that Actually, thinking about it more. I dunno if that's acceptable for inclusion into Go Gators??! |
Thought y'all might be interesting in this: If merged, you'll be able to feed a matplotlib axes object a list of dictionaries "stats" describing the boxplots via a |
@y-p -- yeah. What I've heard is that |
@phobson new champion for a combined viz library? (built on pandas of course) :) |
@y-p I can't say for certain what the final fate of it is. My impression, however, is that it'll be thrown out. Personally, I think pandas/pydata people/we should be equally supporting/endorsing/contributing to seaborn and python-ggplot. I got in early on matplotlib straight from using matlab during my stint in academia -- which is to say I'm comfortable using it directly and I probably won't ever get my head around ggplot-esque APIs. However, I think having both styles of API is crucial to growing the python-science/data/viz community. |
poignant comment made a few days ago re two valid approaches: mpl recipes vs. ggplot. Note: @phobson just beat me to it :) |
@phobson - yeah, definitely think both are good efforts. I find ggplot Not totally clear where that leaves us in terms of the visualization PRs we |
So should I move Olga Botvinnik On Mon, Dec 16, 2013 at 9:56 AM, y-p notifications@github.com wrote:
|
You might, I've been thinking of doing the same thing except targeting ggplot. |
Won't fix |
Some basic code to display daily OHLCV data import pandas as pd
from pandas.compat import StringIO
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
def plot_candlestick(df, ax=None, fmt="%Y-%m-%d"):
if ax is None:
fig, ax = plt.subplots()
idx_name = df.index.name
dat = df.reset_index()[[idx_name, "Open", "High", "Low", "Close"]]
dat[df.index.name] = dat[df.index.name].map(mdates.date2num)
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter(fmt))
plt.xticks(rotation=45)
_ = candlestick_ohlc(ax, dat.values, width=.6, colorup='g', alpha =1)
ax.set_xlabel(idx_name)
ax.set_ylabel("OHLC")
return ax
data="""Date,Stock,Open,High,Low,Close,Volume
2016-09-29,KESM,7.92,7.98,7.92,7.97,149400
2016-09-30,KESM,7.96,7.97,7.84,7.9,29900
2016-10-04,KESM,7.8,7.94,7.8,7.93,99900
2016-10-05,KESM,7.93,7.95,7.89,7.93,77500
2016-10-06,KESM,7.93,7.93,7.89,7.92,130600
2016-10-07,KESM,7.91,7.94,7.91,7.92,103000"""
df = pd.read_csv(StringIO(data), index_col='Date', parse_dates=True)
ax = plot_candlestick(df)
print(ax)
plt.tight_layout()
#plt.savefig("candle.png")
plt.show() Related SO questions: |
@femtotrader you are just plotting a box-plot. http://pandas.pydata.org/pandas-docs/stable/visualization.html#box-plots |
Not sure if possible to do in a generic way. But maybe will make sense once we have an OHLC aggregator implemented
The text was updated successfully, but these errors were encountered: