#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()
当它打开一个窗口时,传说是不正确的,甚至认为它支持!
当您点击运行,图例框在那里,但不正确,首先特斯拉收盘价格支持是青色,但是一条绿色线,价格上涨是好的,但不是价格下跌。
1条答案
按热度按时间68bkxrlz1#
我在最新的mplfinance模块中复制了您的代码。事实上,图例中的句柄和标签是错误的,githjub上有一个解决方案可以解决这个先例,但它不适用于这种情况。因此,作为一种变通方法,我创建了一个新的红色补丁,并重用现有的句柄来创建一个句柄。我不确定这种方法是否是最好的。我用于定制的示例是styles和additional graphs。
问题图例