I have the following code which produces finance plot:
JavaScript
x
6
1
from pandas_datareader import data as web
2
import pandas as pd
3
import mplfinance as mpf
4
df = web.DataReader('goog','yahoo', start="2021-07-3", end="2021-09-12")
5
mpf.plot(df, style='charles', type = 'candle', volume=True, figratio=(12,8), title='large title', savefig = 'testimage.jpg')
6
But the image which it generated left a lot of space on all side, so i include the parameter tight_layout=True
however now i a getting an image which looks like this:
The title is inside the image. Could you advise how can i change the margin where the left and right are tight and title is on top. Also how can you add a secondary title to the image
Advertisement
Answer
Extra configuration can be passed to position the title when title option is passed as a dictionary.
For example,
JavaScript
1
10
10
1
config = dict(
2
style="charles",
3
type="candle",
4
volume=True,
5
figratio=(12,8),
6
title={"title": "large title", "y": 1},
7
tight_layout=True
8
)
9
fig = mpf.plot(df, **config)
10
To add a secondary title. You’ll need control of the plot axis.
You can write:
JavaScript
1
23
23
1
ax1 = plt.axes([0,0,1,0.4])
2
ax1.set_title("Volume")
3
ax1.yaxis.set_label_position("right")
4
ax1.yaxis.tick_right()
5
6
ax2 = plt.axes([0,0.6,1,0.4])
7
ax2.set_title("Price")
8
ax2.yaxis.set_label_position("right")
9
ax1.yaxis.tick_right()
10
11
12
config = dict(
13
style="charles",
14
type="candle",
15
volume=ax1,
16
ax=ax2,
17
figratio=(12,8),
18
num_panels=2,
19
tight_layout=True
20
)
21
22
fig = mpf.plot(df, **config)
23