如何使用 matplotlib 在图表中显示图例
How to get legend to show in graph using matplotlib
编写了一个简单的程序来创建股票的指数移动平均线。代码如下:
import yfinance as yf
import pandas_datareader as pdr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
import datetime as dt
yf.pdr_override()
style.use('ggplot')
startyear = 2019
startmonth = 1
startday = 1
start = dt.datetime(startyear, startmonth, startmonth)
end = dt.datetime.now()
stock = input('Enter stock ticker: ')
df = pdr.get_data_yahoo(stock, start, end)
emasUsed = [3, 5, 8, 10, 13, 15, 30, 35, 40, 45, 50, 60]
for x in emasUsed:
ema = x
df['EMA_'+str(ema)] = df['Adj Close'].ewm(span=ema, adjust=True).mean()
df['EMA_'+str(ema)].plot()
plt.show()
我想绘制移动平均线图,但无法显示图例,除非我像这样在单独的行上绘制 EMA:
df[['EMA_3', 'EMA_5', 'EMA_8', etc...]].plot()
这显然需要做很多工作,尤其是当我想说添加或更改我想要获得的 EMA 时。
有没有办法让图例显示出来而无需手动输入每个 EMA?
谢谢,
旦
您可以在绘图之前获取轴,然后用它来绘制图例。绘图完成后调用它就是这样。
import yfinance as yf
import pandas_datareader as pdr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
import datetime as dt
yf.pdr_override()
style.use('ggplot')
startyear = 2019
startmonth = 1
startday = 1
start = dt.datetime(startyear, startmonth, startmonth)
end = dt.datetime.now()
#stock = input('Enter stock ticker: ')
stock = 'SPY'
df = pdr.get_data_yahoo(stock, start, end)
emasUsed = [3, 5, 8, 10, 13, 15, 30, 35, 40, 45, 50, 60]
fig, ax = plt.subplots(figsize=(10, 8)) # get the axis and additionally set a bigger plot size
for x in emasUsed:
ema = x
df['EMA_'+str(ema)] = df['Adj Close'].ewm(span=ema, adjust=True).mean()
df['EMA_'+str(ema)].plot()
legend = ax.legend(loc='upper left') # Here's your legend
plt.show()
结果:
编写了一个简单的程序来创建股票的指数移动平均线。代码如下:
import yfinance as yf
import pandas_datareader as pdr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
import datetime as dt
yf.pdr_override()
style.use('ggplot')
startyear = 2019
startmonth = 1
startday = 1
start = dt.datetime(startyear, startmonth, startmonth)
end = dt.datetime.now()
stock = input('Enter stock ticker: ')
df = pdr.get_data_yahoo(stock, start, end)
emasUsed = [3, 5, 8, 10, 13, 15, 30, 35, 40, 45, 50, 60]
for x in emasUsed:
ema = x
df['EMA_'+str(ema)] = df['Adj Close'].ewm(span=ema, adjust=True).mean()
df['EMA_'+str(ema)].plot()
plt.show()
我想绘制移动平均线图,但无法显示图例,除非我像这样在单独的行上绘制 EMA:
df[['EMA_3', 'EMA_5', 'EMA_8', etc...]].plot()
这显然需要做很多工作,尤其是当我想说添加或更改我想要获得的 EMA 时。
有没有办法让图例显示出来而无需手动输入每个 EMA?
谢谢, 旦
您可以在绘图之前获取轴,然后用它来绘制图例。绘图完成后调用它就是这样。
import yfinance as yf
import pandas_datareader as pdr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
import datetime as dt
yf.pdr_override()
style.use('ggplot')
startyear = 2019
startmonth = 1
startday = 1
start = dt.datetime(startyear, startmonth, startmonth)
end = dt.datetime.now()
#stock = input('Enter stock ticker: ')
stock = 'SPY'
df = pdr.get_data_yahoo(stock, start, end)
emasUsed = [3, 5, 8, 10, 13, 15, 30, 35, 40, 45, 50, 60]
fig, ax = plt.subplots(figsize=(10, 8)) # get the axis and additionally set a bigger plot size
for x in emasUsed:
ema = x
df['EMA_'+str(ema)] = df['Adj Close'].ewm(span=ema, adjust=True).mean()
df['EMA_'+str(ema)].plot()
legend = ax.legend(loc='upper left') # Here's your legend
plt.show()
结果: