【问题标题】:How do I scale mplfinance graph within Tkinter?如何在 Tkinter 中缩放 mplfinance 图?
【发布时间】:2022-01-23 15:02:27
【问题描述】:

my post earlier 的帮助下,我成功地将我的图表变成了烛台图表。但是我似乎无法调整它的大小。

旧图表:

新图表:

我想将其调整为以前的大小。我尝试将以下内容添加到 rcparams 但没有效果:

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

我的旧相关代码:

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)

我当前的代码:

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)

在小部件上尝试.pack(expand=True) 时,它也不起作用。

编辑: 感谢 Goldfarb 先生,我让图表变得更大,但它仍然不合适。有没有办法可以应用我通常添加的figure.tight_layout()figure.subplots_adjust(left=0.05, right=1.0, bottom=0.0, top=1.0)

【问题讨论】:

  • 尝试在最后一行使用包管理器而不是位置管理器,即FigureCanvasTkAgg(figure, self).get_tk_widget().pack(expand = true),看看它是否有效(expand = true 是可选的。它只是告诉小部件占用所有可用空间) .但是,请记住,这可能会导致您在使用不同的几何管理器时对定位小部件进行微小的更改。
  • @Grasshopper 这对图表的比例没有影响,请参阅我对帖子的编辑。
  • 为了清楚起见(我对 Tkinter 不是很熟悉),关于 this image herethis other image here,这两个图像显示 三个 单独的 matplotlib 图形嵌入到 Tkinter 屏幕中? (问是因为我没有看到“Volume($)”图的代码,也没有看到“RSI(%)”图的代码。只是想确认我正确理解了我正在查看的内容。

标签: python matplotlib mplfinance


【解决方案1】:

您可以在致电mpf.plot() 时使用 kwarg figsize=(width,height) 调整图形大小。而不是:

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

试试

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

关于Figure.tight_layout()Figure.subplots_adjust() ... mplfinance 使用matplotlib 的Figure.add_axes() 创建Axes 对象,显然,虽然add_axes() 是matplotlib 的一部分,但add_axes() 创建的Axes 与matplotlib 的不兼容tight_layout。我不知道subplots_adjust() 是否也可以工作(因为我从未尝试过)。

也就是说,mplfinance 实现了它自己的tight_layout。调用 mpf.plot() 时只需设置 kwarg tight_layout=True。首先尝试,但如果这不能满足您想要完成的目标,请尝试使用the scale_padding kwarg

【讨论】:

  • 谢谢 Goldfarb 先生,这行得通,但是我仍然对紧凑的布局有问题,无法正确适应,这已被编辑到我的问题中。
猜你喜欢
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 2018-09-08
  • 2021-07-02
  • 2021-09-19
  • 2016-10-13
相关资源
最近更新 更多