Typical Usage¶
In [6]:
Copied!
from mplchart.chart import Chart
from mplchart.samples import sample_prices
from mplchart.primitives import Candlesticks, Volume, Pane, LinePlot
from mplchart.indicators import SMA, RSI, MACD
prices = sample_prices()
from mplchart.chart import Chart
from mplchart.samples import sample_prices
from mplchart.primitives import Candlesticks, Volume, Pane, LinePlot
from mplchart.indicators import SMA, RSI, MACD
prices = sample_prices()
SVG charts in notebooks¶
By default notebooks display charts as PNG images. For sharper inline charts, switch the display format to SVG at the top of your notebook — as this notebook does:
In [7]:
Copied!
from matplotlib_inline.backend_inline import set_matplotlib_formats
set_matplotlib_formats("svg")
from matplotlib_inline.backend_inline import set_matplotlib_formats
set_matplotlib_formats("svg")
or equivalently with the IPython magic %config InlineBackend.figure_formats = {"svg"}. This is notebook display configuration (an IPython/matplotlib feature, not an mplchart one) — it has no effect on scripts or on exporting charts to files.
Plot chart with indicators¶
In [8]:
Copied!
(Chart(prices, title="Example Chart", max_bars=250)
.plot(
Candlesticks(), Volume(), SMA(50), SMA(200),
Pane("above", yticks=(30, 50, 70)),
LinePlot(RSI(), overbought=70, oversold=30),
Pane("below"),
MACD(),
)
.show()
)
(Chart(prices, title="Example Chart", max_bars=250)
.plot(
Candlesticks(), Volume(), SMA(50), SMA(200),
Pane("above", yticks=(30, 50, 70)),
LinePlot(RSI(), overbought=70, oversold=30),
Pane("below"),
MACD(),
)
.show()
)
What about polars?¶
This walkthrough uses pandas data, but mplchart works the same with polars DataFrames — the charting layer is identical, with polars expressions taking the place of indicators. See the Pandas and polars backends and How expressions work tutorials.