1

With the help of my post earlier, I have managed to turn my chart into a candlestick one. However I cannot seem to adjust the size of it.

Old chart: enter image description here

New Chart: enter image description here

I'd like to resize it to how it was before. I have tried adding the following to rcparams but it had no effect:

"figure.figsize": figure_size,
"savefig.bbox": "tight"

My old relevant code:

figure = plt.Figure(figsize=(0.7 * res_width / 100, 0.45 * res_height / 100), facecolor=BACKGROUND_COLOUR)
ax = figure.add_subplot(111, fc=BACKGROUND_COLOUR)
figure.tight_layout()
figure.subplots_adjust(left=0.05, right=1.0, bottom=0.0, top=1.0)
FigureCanvasTkAgg(figure, self).get_tk_widget().place(relx=0.02, rely=0.27)

My current code:

market_colours = mpf.make_marketcolors(up="g", down="r",
                                           edge=background_colour,
                                           wick=line_colour)

style_dict = {"xtick.color": line_colour,
                  "ytick.color": line_colour,
                  "xtick.labelcolor": text_colour,
                  "ytick.labelcolor": text_colour,
                  "axes.spines.top": False,
                  "axes.spines.right": False,
                  "axes.labelcolor": text_colour}

style = mpf.make_mpf_style(marketcolors=market_colours,
                               facecolor=background_colour,
                               edgecolor=line_colour,
                               figcolor=background_colour,
                               gridcolor=line_colour,
                               gridstyle="--",
                               rc=style_dict)

figure, ax = mpf.plot(df, type="candle", returnfig=True, style=style, TEXT_COLOUR))
FigureCanvasTkAgg(figure, self).get_tk_widget().place(relx=0.02, rely=0.27)

When trying .pack(expand=True) on the widget it also doesn't work. enter image description here

Edit: Thanks to Mr Goldfarb I have got the graph to be bigger, but it still does not fit right. Is there a way I can apply the figure.tight_layout() along with figure.subplots_adjust(left=0.05, right=1.0, bottom=0.0, top=1.0) that I usually add? enter image description here

  • Try using the pack manager instead of the place manager in the last line i.e. ```FigureCanvasTkAgg(figure, self).get_tk_widget().pack(expand = true)``` and see if it works (```expand = true``` is optional. It just tells the widget to take all the available space). But, remember, this may cause you to make minor changes in positioning widgets as you are, after all, using a different geometry manager. – Grasshopper Jan 23 '22 at 15:17
  • @Grasshopper That had no effect on the scale of the graph, please see my edit to the post. – Royal_Scribblz Jan 24 '22 at 10:45
  • Just to be clear (I am not very familiar with Tkinter), regarding [this image here](https://i.stack.imgur.com/C3zj6.jpg) and [this other image here](https://i.stack.imgur.com/RgFl1.jpg), is it correct that both images show **three** separate matplotlib **Figures** embedded into a Tkinter screen? (Asking because I don't see the code for the "Volume($)" plot nor for the "RSI(%)" plot. Just want to confirm that I am understanding correctly what I am looking at. – Daniel Goldfarb Jan 25 '22 at 17:13

1 Answers1

2

You can adjust the figure size with kwarg figsize=(width,height) in your call to mpf.plot(). Instead of:

figure, ax = mpf.plot(df, type="candle", returnfig=True, style=style, TEXT_COLOUR)

Try

figure, ax = mpf.plot(df, type="candle", returnfig=True, style=style, TEXT_COLOUR,
                      figsize=(0.7*res_width/100, 0.45*res_height/100) )

Regarding Figure.tight_layout() and Figure.subplots_adjust() ... mplfinance uses matplotlib's Figure.add_axes() to create Axes objects, and apparently, although add_axes() is part of matplotlib, the Axes that are created by add_axes() are incompatible with matplotlib's tight_layout. I do not know if subplots_adjust() will work either (as I've never tried it).

That said, mplfinance implements it's own tight_layout. Just set kwarg tight_layout=True when calling mpf.plot(). Try that first, but if that doesn't satisfy what you are trying to accomplish, then try using the scale_padding kwarg.

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • Thank you Mr Goldfarb, this worked, however I'm still having issues with the tight layout for it to fit correctly, this has been edited into my question. – Royal_Scribblz Jan 27 '22 at 15:04