highfestiva/finplot

secondary_y or arbitrary data overlay?

jabowery opened this issue · 10 comments

Is there some way to plot more than one data series with a secondary y-axis scale (as with secondary_y with matplotlib) or at least overlay arbitrary data on the same plot (with the same datetime index)? I looked in the code at the Pandas-related PlotDf but doing a dir on the return doesn't show anything relevant AFAIK.

I've not implemented any such support yet unfortunately. One solution to just scale the second plot (and ignore its amplitude) is to make it into an overlay. Here comparing Walmart to Twitter:

#!/usr/bin/env python3

import finplot as fplt
import yfinance as yf


df1 = yf.download('WMT', '2020-09-01')
df2 = yf.download('TWTR', '2020-09-01')

ax = fplt.create_plot()
p1 = fplt.candlestick_ochl(df1[['Open','Close','High','Low']], ax=ax)
p2 = fplt.candlestick_ochl(df2[['Open','Close','High','Low']], ax=ax.overlay(scale=1.0))
p2.x_offset = 0.1
p2.colors.update(dict(
            bull_shadow = '#388d53',
            bull_frame  = '#205536',
            bull_body   = '#52b370',
            bear_shadow = '#d56161',
            bear_frame  = '#5c1a10',
            bear_body   = '#e8704f'))
fplt.show()

image

Does this suit your needs, or do you need another axis too?

Added support for multiple Y-axes in def7203. Modify the above like so:

p2 = fplt.candlestick_ochl(df2[['Open','Close','High','Low']], ax=ax.overlay(scale=1.0, y_axis='linear'))

image

All additional Y-axes end up on the right-hand side. Possibly I'll address positioning in the future, but don't really like too much configuration, ending up in bugs or difficulty understanding the setup of a user with problems. I already think there is a bit much... :)

Many moving parts are definitely to be avoided -- I like your philosophy. That said, there is a clear need to incorporate user-defined technical indicators as overlays with their own scales. That's more or less what I'm after.

If there is anything lacking, ask - otherwise close?

I've got another idea for what I hope would be a relatively configuration-free way to handle multiple y-axes. But let's close this one. Thanks!

Workaround and fix is in so closed.

  1. Is it possible to assign the right y-axis to one symbol and the left y-axis to the second symbol? Or if there are 3 plots (line) on one pane, two lines assign to the right y-axis and one to the left y-axis?
  2. Can I make the candle body color transparent? So, when they overlay, both symbol charts are seen (for example, look at the chart above - when they intersect, one is not visible behind the other)
  1. Using overlay, you can show a different symbol with a left-hand axis, see below. There are currently no support for more than two axes, but feel free to create a pull request.
  2. Yes, but it comes with some drawbacks, as the shadow is drawn behind the candle body, so if you make it transparent, you'll see a vertical line through the body. It might be better than nothing, so try it out.
#!/usr/bin/env python3

import finplot as fplt
import yfinance as yf

spx = yf.download('SPY', period='3y')
btc = yf.download('BTC-USD', period='3y')
btc = btc.loc[spx.index, :] # remove non-trading stock days from btc

ax = fplt.create_plot('SPX vs. BTC')

cols = 'Open Close High Low'.split()

spx[cols].plot(kind='candle', ax=ax)
ax_overlay = ax.overlay(scale=1, yaxis='linear')
btc_plot = btc[cols].plot(kind='candle', ax=ax_overlay)

btc_plot.x_offset = -.1
ax_overlay.vb.setZValue(10)

btc_plot.colors.update(dict(
  bull_shadow = '#008000a0',
  bull_frame  = '#008000a0',
  bull_body   = '#008000a0',
  bear_shadow = '#000000a0',
  bear_frame  = '#000000a0',
  bear_body   = '#000000a0'))


fplt.show()

image

sorry, I didn't get what actually did the transparency? This code: ax_overlay.vb.setZValue(10) ?

Ah, sorry, the last byte of the color component is transparency, in this case a0.