Rendering Charts to File Formats¶
Render a chart to SVG, PNG, or JPG bytes using chart.render(fmt).
In [1]:
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
from matplotlib_inline.backend_inline import set_matplotlib_formats
set_matplotlib_formats("svg")
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
from matplotlib_inline.backend_inline import set_matplotlib_formats
set_matplotlib_formats("svg")
In [2]:
Copied!
prices = sample_prices()
prices
prices = sample_prices()
prices
Out[2]:
| open | high | low | close | volume | |
|---|---|---|---|---|---|
| date | |||||
| 1980-12-12 | 0.098389 | 0.098817 | 0.098389 | 0.098389 | 469033600 |
| 1980-12-15 | 0.093684 | 0.093684 | 0.093256 | 0.093256 | 175884800 |
| 1980-12-16 | 0.086839 | 0.086839 | 0.086412 | 0.086412 | 105728000 |
| 1980-12-17 | 0.088550 | 0.088978 | 0.088550 | 0.088550 | 86441600 |
| 1980-12-18 | 0.091118 | 0.091545 | 0.091118 | 0.091118 | 73449600 |
| ... | ... | ... | ... | ... | ... |
| 2025-11-11 | 269.809998 | 275.910004 | 269.799988 | 275.250000 | 46208300 |
| 2025-11-12 | 275.000000 | 275.730011 | 271.700012 | 273.470001 | 48398000 |
| 2025-11-13 | 274.109985 | 276.700012 | 272.089996 | 272.950012 | 49602800 |
| 2025-11-14 | 271.049988 | 275.959991 | 269.600006 | 272.410004 | 47399300 |
| 2025-11-17 | 268.815002 | 270.489990 | 265.730011 | 267.459991 | 42862157 |
11325 rows × 5 columns
In [3]:
Copied!
max_bars = 250
chart = Chart(prices, title="Example", max_bars=max_bars).plot(
Candlesticks(use_bars=False),
SMA(50),
SMA(200),
Volume(),
Pane("above", yticks=(30, 50, 70)),
LinePlot(RSI(), overbought=70, oversold=30),
Pane("below"),
MACD(),
)
max_bars = 250
chart = Chart(prices, title="Example", max_bars=max_bars).plot(
Candlesticks(use_bars=False),
SMA(50),
SMA(200),
Volume(),
Pane("above", yticks=(30, 50, 70)),
LinePlot(RSI(), overbought=70, oversold=30),
Pane("below"),
MACD(),
)
In [4]:
Copied!
data = chart.render("svg")
print(data[:20] + b"...")
data = chart.render("svg")
print(data[:20] + b"...")
b'<?xml version="1.0" ...'
In [5]:
Copied!
data = chart.render("png")
print(data[:20] + b"...")
data = chart.render("png")
print(data[:20] + b"...")
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x04\xb0...'
In [ ]:
Copied!