【问题标题】:mplfinance plot not saving when called outside of plot command [duplicate]在绘图命令之外调用 mplfinance 绘图不保存 [重复]
【发布时间】:2021-09-10 11:25:08
【问题描述】:

我是 matplotlib 的新手。我正在努力在第一个 .plot 调用中自定义我的情节元素。例如,ylabel 有效,但 xlabel 无效。我希望我可以将 savefig 命令分开,以便我可以在第一个 .plot 调用和 savefig 之间添加/修改绘图元素调用(因为我在网上看到的所有示例似乎都是创建情节然后单独修改元素,即 fplt.xlabel("Blah")

我注意到我在网上找到的很多绘图示例(用于折线图等)分别提供了所有 x 和 y 值,但我喜欢我使用的绘图技术,因为它自动使用高、低等来显示制作蜡烛。

那么为什么这段代码可以工作:

            fplt.plot(
                    dft,
                    type="candle",
                    style='charles',
                    addplot=extraPlot2,
                    ylabel=stock,
#                    xlabel="Blah", <= removed as doesn't work
                    figratio=(10, 6), volume=True,
                    savefig=dict(
                        fname=folder2,
                        bbox_inches="tight"
                    )
            )

但是这段代码没有(即使添加了哈希):

            fplt.plot(
                    dft,
                    type="candle",
                    style='charles',
                    addplot=extraPlot2,
                    ylabel=stock,
#                    xlabel="Blah", <= removed as doesn't work
                    figratio=(10, 6), volume=True,
            )
#            fplt.xlabel("Blah") <= would like to do this if I can get savefig to work
#           fplt.xticks(rotation=45) <= i'd also like to do stuff like this
            fplt.savefig(folder2)

我已尝试将 fplt.plot 设为变量并以该变量为目标,但没有成功。 对任何糟糕的术语表示歉意,我对此很陌生。

编辑:在下面添加了导入以供参考。而且我意识到为什么 xlabel 现在不起作用了,因为我看到我正在导入它。

import datetime as dt
from matplotlib.pyplot import bar, xlabel
from numpy import False_, NaN
import pandas as pd
from pandas_datareader import data as pdr
import yfinance as yf
from tkinter import EXCEPTION, Tk
from tkinter.filedialog import askopenfilename
import os
from pandas import ExcelWriter
import mplfinance as fplt
from pathlib import Path

【问题讨论】:

标签: python matplotlib mplfinance


【解决方案1】:

您可以导入 Matplotlib pyplot 模块并使用gcf()(“获取当前图形”)函数返回一个Figure 对象。具有savefig() 方法的是Figure 对象。例如,

from matplotlib import pyplot as plot

# your code here

fig = plt.gcf()  # gcf is "get current figure"
fig.savefig(folder2)

查看this mplfinance issue,您也可以使用以下命令返回图形对象:

fig, axlist = fplt.plot(..., returnfig=True)

# save figure
fig.savefig(folder2)

【讨论】:

  • 现已添加导入。我认为 mplfinance 处理烛台元素。也许我应该使用import mplfinance as mplf; from matplotlib import pyplot as fplt
  • 您可以使用import mplfiance as fplt,但您还需要使用from matplotlib import pyplot as plt,而不是fig = plt.gcf()
  • 我会相应地编辑答案。
  • 谢谢马特。现在使用import mplfinance as fplt from matplotlib; import pyplot as plt 绘图工作。但是,当我尝试进一步自定义图表时,即plt.xticks(rotation = 90),这会被忽略。我试过把它放在不同的位置,但没有运气(也没有抛出错误)。
  • PS 我知道我可以使用 mltfinance xrotation=90,但我希望能够访问 matplotlib 自定义,因为似乎有更多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 2019-04-10
相关资源
最近更新 更多