当我在 class 中执行相同的语句时,为什么不绘制图表?

Why doesn't plotly show the graph, when I execute the same statements inside a class?

我正在学习 classes。下面的脚本使用 plotly express 正确显示了图形,但如果我将它作为方法集成到 class 中,则不会。 我在下面显示 classes。对于第一个,我们从 Yahoo! 导入报价对于第二个,我们尝试表示进口标准化价格的图表。

import pandas as pd
import pandas_datareader as pdr 
import datetime as dt
from datetime import date
from plotly.offline import iplot
import plotly.express as px 

class ImportadorCotizaciones:

    def __init__(self):
        self.cotizaciones = None
        self.start = "2000-1-4"
        self.end = date.today()
        self.cotizaciones = None

    def Importar_cotizaciones(self):

        dicc_tickers = {"IBE.MC":"Iberdrola", "TEF.MC":"Telefonica", "^IBEX":"Ibex35" }
        dfs = []
        nombres = []

        for (k,v) in dicc_tickers.items():
            self.cotizaciones_de_ticker = pdr.DataReader(k, 'yahoo', self.start, self.end)
            self.cotizaciones_de_ticker = self.cotizaciones_de_ticker[["Close"]]
            self.cotizaciones_de_ticker = self.cotizaciones_de_ticker.rename(columns={"Close": v})

            dfs.append(self.cotizaciones_de_ticker)

        dfs = iter(dfs)
        self.cotizaciones = next(dfs)
        for df_ in dfs:
            self.cotizaciones = self.cotizaciones.merge(df_, on='Date')

class Indicadores:
    def __init__(self, importador):
        self.importador = importador 
    
    def dibujar_grafico(self):
        self.aux_val_ind = importador.cotizaciones[["Iberdrola", "Ibex35"]].pct_change().dropna() 
        df = self.aux_val_ind.copy(deep=True)
        df['Media'] = df.mean(axis = 1) 
        # Usando plotly.express       
        px.line((df + 1).cumprod() ,y=df.columns ,title=f"\nValor de 1€ invertido desde el { importador.start}  hasta el {importador.end} ")

importador = ImportadorCotizaciones()
importador.Importar_cotizaciones()
importador.cotizaciones[:3] 

indicadores = Indicadores(importador)
indicadores.dibujar_grafico()

在 class 之外运行的脚本是:

# Usando plotly.express
from plotly.offline import iplot
import plotly.express as px

start = "2000-1-4"
end = date.today()
aux_val_ind = importador.cotizaciones[["Iberdrola", "Ibex35"]].pct_change().dropna() 
df = aux_val_ind.copy(deep=True)
df['Media'] = df.mean(axis = 1) 

px.line((df + 1).cumprod() ,y=df.columns ,title=f"\nValor actual de 1€ invertido el {start} ")

只有一期,你错过了return这个数字。

class Indicadores:
    def __init__(self, importador):
        self.importador = importador 
    
    def dibujar_grafico(self):
        self.aux_val_ind = importador.cotizaciones[["Iberdrola", "Ibex35"]].pct_change().dropna() 
        df = self.aux_val_ind.copy(deep=True)
        df['Media'] = df.mean(axis = 1) 
        # Usando plotly.express       
        return px.line((df + 1).cumprod() ,y=df.columns ,title=f"\nValor de 1€ invertido desde el { importador.start}  hasta el {importador.end} ")