gpt4 book ai didi

python - 如何在 matplotlib finance 中保存烛台图表

转载 作者:行者123 更新时间:2023-12-01 23:36:42 33 4
gpt4 key购买 nike

我制作了一些 python 脚本来使用 mpl_finance 创建烛台图表。我已经成功制作了烛台图表,但为了我的生命,我找不到将图表保存为 png 的方法。这是我的代码:

!pip install yfinance
!pip install mplfinance
!pip install bokeh
!pip install mpl-finance
# https://towardsdatascience.com/trading-toolbox-03-ohlc-charts-95b48bb9d748
from mplfinance.original_flavor import candlestick_ohlc
import mplfinance as fplt

import yfinance as yf
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
from datetime import timedelta
import matplotlib.dates as mdates
from bokeh.plotting import figure,show,output_file
import matplotlib.pyplot as plt
import mpl_finance as mpf #

yf.pdr_override()
start = dt.datetime.now() - timedelta(60)
now = dt.datetime.now()

stock = "BAC"

df = pdr.get_data_yahoo(stock ,start , now)

# Convert yahoo finance data to readable to mplfinance
df.index = pd.to_datetime(df.index)
dvalues = df[['Open','High', 'Low','Close']].values.tolist()

pdates = mdates.date2num(df.index)
ohlc = [ [pdates[i]] + dvalues[i] for i in range(len(pdates)) ]

plt.style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize = (16,12))


mpf.candlestick_ohlc(ax, ohlc, width=0.4, colorup='green', colordown='red') # i want to save this chart.
plt.savefig('amzn.png') # i have tried this but not avail

最佳答案

这可以仅使用新的 mplfinance 更简单地完成:

import mplfinance as mpf
import pandas as pd
import datetime as dt
import pandas_datareader as pdr

now = dt.datetime.now()
start = now - dt.timedelta(60)

stock = "AMZN"
filename = stock.lower()+'.png'

df = pdr.get_data_yahoo(stock ,start , now)
mpf.plot(df,type='candle',style='yahoo',savefig=filename)

结果: enter image description here

关于python - 如何在 matplotlib finance 中保存烛台图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65487952/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com