matplotlib 传说是不工作与烛台

7kqas0il  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(122)
#Import
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
from mplfinance.original_flavor import candlestick_ohlc
import matplotlib.dates as mdates

#Dates to get stocks
start_date = "2020-01-01"
end_date = "2023-05-08"

#Get to the site
tesla_data = yf.download("TSLA", start = start_date, end = end_date)

#Get info off the site
tesla_weekly_data = tesla_data.resample("W").agg({"Open":"first", "High":"max", "Low":"min", "Close":"last", "Volume":"sum"}).dropna()
tesla_weekly_data.reset_index(inplace = True)
tesla_weekly_data["Date"] = tesla_weekly_data["Date"].map(mdates.date2num)

#Configer the plot
fig, ax = plt.subplots(figsize=(16,8))

#Show Canddlestick
candlestick_ohlc(ax, tesla_weekly_data.values, width = 5, colorup = "green", colordown = "red")

#Fonts
font1 = {"family":"serif", "color":"black", "size":40, "fontweight":"bold"}
font2 = {"family":"serif", "color":"black", "size":20, "fontweight":"bold"}

#Show Descripton of title and labels
plt.xlabel("Date", fontdict = font2)
plt.ylabel("Price",fontdict = font2)
plt.title("Tesla Stock Prices", fontdict = font1)

#Show the stocks line
plt.plot(tesla_data["Close"], color = "cyan", linewidth = 2)

#Add legend
plt.legend({"Tesla Close Prices":"cyan", "Price Up":"green", "Price Down":"red"})

#Show the plot
plt.show()

当它打开一个窗口时,传说是不正确的,甚至认为它支持!
当您点击运行,图例框在那里,但不正确,首先特斯拉收盘价格支持是青色,但是一条绿色线,价格上涨是好的,但不是价格下跌。

68bkxrlz

68bkxrlz1#

我在最新的mplfinance模块中复制了您的代码。事实上,图例中的句柄和标签是错误的,githjub上有一个解决方案可以解决这个先例,但它不适用于这种情况。因此,作为一种变通方法,我创建了一个新的红色补丁,并重用现有的句柄来创建一个句柄。我不确定这种方法是否是最好的。我用于定制的示例是stylesadditional graphs

import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import mplfinance as mpf
import matplotlib.dates as mdates

#Dates to get stocks
start_date = "2020-01-01"
end_date = "2023-05-08"

tesla_data = yf.download("TSLA", start = start_date, end = end_date)
tesla_weekly_data = tesla_data.resample("W").agg({"Open":"first", "High":"max", "Low":"min", "Close":"last", "Volume":"sum"}).dropna()

#Fonts
font1 = {"family":"serif", "color":"black", "size":40, "fontweight":"bold"}
font2 = {"family":"serif", "color":"black", "size":20, "fontweight":"bold"}
title = "\nTesla Stock Prices"
apd = mpf.make_addplot(tesla_weekly_data['Close'], color='cyan', linewidths=2)
mc = mpf.make_marketcolors(up='g',down='r')
s  = mpf.make_mpf_style(marketcolors=mc)

fig, axes = mpf.plot(tesla_weekly_data,
                       figratio=(16,8),
                       type='candle',
                       style=s,
                       addplot=apd,
                       update_width_config=dict(candle_width=1.2),
                       returnfig=True
                      )

axes[0].set_title(title, **font1)
axes[0].set_ylabel('Prise', **font2)
axes[0].set_xlabel('Date', **font2)

axes[0].legend([None]*(len(apd)+2))
handles = axes[0].get_legend().legend_handles
red_patch = mpatches.Patch(color='red')
axes[0].legend(handles=[handles[1], red_patch, handles[-1]], labels={"Price Up":"green", "Price Down":"red","Tesla Close Prices":"cyan"})

plt.show()

问题图例

相关问题