Snippets - highfestiva/finplot GitHub Wiki
Background color
# finplot uses no background (i.e. white) on even rows and a slightly different color on odd rows.
# Set your own before creating the plot.
fplt.background = '#ff0' # yellow
fplt.odd_plot_background = '#f0f' # purple
fplt.plot(df.Close)
fplt.show()
Unordered time series
finplot requires time-ordered time series - otherwise you'll get a crosshair and an X-axis showing the millisecond epoch instead of the actual time. See my comment here and issue 50 for more info.
It is also imperative that you either put your datetimes in your index, or in the first column. If your
datetime is in the first column, you normally want to have a zero-based range index,
df.reset_index(drop=True)
, before plotting.
Restore the zoom at startup
# By default finplot shows all or a subset of your time series at startup. To store/restore zoom position:
fplt.autoviewrestore()
fplt.show() # will load zoom when showing, and save zoom when closing
Time zone
# Pandas normally reads datetimes in UTC time zone.
# finplot by default use the local time zone of your computer (for crosshair and X-axis)
from dateutil.tz import gettz
fplt.display_timezone = gettz('Asia/Jakarta')
# ... or in UTC = "display same as timezone-unaware data"
import datetime
finplot.display_timezone = datetime.timezone.utc
Scatter plot with X-offset
To offset your scatter markers (say 0.2 time intervals to the left), see my comment here.
Align X-axes
See issue 27, and possibly (rarely a problem) issue 4.
Disable zoom/pan sync between axes
# finplot assumes all your axes are in the same time span. To decouple the zoom/pan link, use:
ax2.decouple()
Move viewport along X-axis (and autozoom)
Use fplt.set_x_pos(xmin, xmax, ax)
. See
examples/animate.py.
Place Region of Interest (ROI) markers
For placing ellipses, see issue 57. For drawing lines, see examples/line.py. (Interactively use Ctrl+drag for lines and Ctrl+mbutton-drag for ellipses.)
More than one Y-axis in same viewbox
fplt.candlestick_ochl(df2['Open','Close','High','Low'](/highfestiva/finplot/wiki/'Open','Close','High','Low'), ax=ax.overlay(scale=1.0, yaxis='linear'))
The scale
parameter means it goes all the way to the top of the axis (volume normally stays at the bottom).
The yaxis
parameter can be one of False
(hidden which is default), 'linear'
or 'log'
.
See issue 52 for more info.
Plot non-timeseries
finplot is made for plotting time series. To plot something different use ax.disable_x_index()
. See second
axis of examples/overlay-correlate.py.
Custom crosshair and legend
S&P500 example shows how to set crosshair texts and update legend text+color as a result of mouse hover.
Custom axes ticks
To use your own labels on the X-axis see comment on issue 50.
If you want to roll your own Y-axis, inherit fplt.YAxisItem
.
Saving screenshot
See examples/line.py. To keep screenshot in RAM see issue 28.
For creating multiple screenshots see issue 71.
Scaling plot heights
See issue 56. Changing the default window size can be
achieved by setting fplt.winw = 900; fplt.winh = 500;
before creating your plot.
Threading
See issue 55.
Titles on axes
See issue 41. To show grid and further adapt axes, etc:
ax.set_visible(crosshair=False, xaxis=False, yaxis=True, xgrid=True, ygrid=True)
Fixing auto-zoom on realtime updates
See issue 131.
Beep
fplt.play_sound('bot-happy.wav') # Ooh! Watch me - I just made a profit!
Keys
Esc
, Home
, End
, g
, Left arrow
, Right arrow
. Ctrl+drag
.
Missing snippets
Plot valign on mouse hover, update an orderbook, etc.