【问题标题】:this make graph function is not working. Any ideas?这使图形功能不起作用。有任何想法吗?
【发布时间】:2021-11-27 14:41:27
【问题描述】:

使用 make_graph 函数绘制 Tesla 股票数据图表,同时为图表提供标题。调用 make_graph 函数的结构是 make_graph(tesla_data, tesla_revenue, 'Tesla')。请注意,该图表仅显示截至 2021 年 6 月的数据。

关于如何回答这个问题的任何想法?我得到了一个功能,下面的功能超出了我的想象。如图所示,我已经复制粘贴了。

def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()

【问题讨论】:

    标签: python pandas plotly stock


    【解决方案1】:

    如果您可以编写此代码,那么我不确定您的问题是什么。如果您没有编写代码,则应说明代码的来源和用途。还建议附上您正在使用的数据样本。 我不知道特斯拉的营收数据是怎么得到的,所以我用股价数据的收盘价和股价数据的成交量作为营收数据。 修复:添加了一个函数调用并将图形的名称更改为volume。

    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    import yfinance as yf
    import pandas as pd
    
    stock_data = yf.download("TSLA", start="2020-01-01", end="2021-09-30", progress=False)
    revenue_data = yf.download("TSLA", start="2020-01-01", end="2021-09-30", progress=False)
    stock_data.reset_index(inplace=True)
    revenue_data.reset_index(inplace=True)
    
    def make_graph(stock_data, revenue_data, stock):
        fig = make_subplots(rows=2, cols=1, 
                            shared_xaxes=True, 
                            subplot_titles=("Historical Share Price", "Historical Revenue"), 
                            vertical_spacing=.3)
        
        stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
        revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
        
        fig.add_trace(go.Scatter(
            x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True),
            y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
        
        fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), 
                                 y=revenue_data_specific.Volume.astype("float"),
                                 name="Volume"), row=2, col=1)
        
        fig.update_xaxes(title_text="Date", row=1, col=1)
        fig.update_xaxes(title_text="Date", row=2, col=1)
        fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
        fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
        
        fig.update_layout(showlegend=False,
                          height=900,
                          title=stock,
                          xaxis_rangeslider_visible=True)
        
        fig.show()
        
    make_graph(stock_data, revenue_data, 'TSLA')
    

    【讨论】:

    • 如果我的回答对你有帮助,评价中上下按钮下方有勾选标记,请点击勾选接受回答。
    猜你喜欢
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多