【问题标题】:How to add labels to subplots in plotly?如何在情节中为子情节添加标签?
【发布时间】:2021-04-14 06:36:06
【问题描述】:

我正在尝试使用 plotly.但是我无法获得正确的 x 和 yaxis 标签。请帮助。我需要 y 标签用于两个绘图,但 xlabel 仅用于底部一个,两个标题也需要一个标题。下面是代码。

** 再问一个问题,如何改变体积图中的线条颜色。谢谢

import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly import tools


stock = 'AAPL'
df = web.DataReader(stock, data_source='yahoo', start='01-01-2019')



def chart_can_vol(df):
    fig = tools.make_subplots(
        rows=3, cols=1,
        specs=[[{"rowspan": 2}], 
               [None],
               [{}]],
        shared_xaxes=True,
    vertical_spacing=0.1)

    fig.add_trace(go.Candlestick(x = df.index,
                                open = df['Open'],
                                close = df['Close'],
                                low = df['Low'],
                                high = df['High']),
                 row = 1, col = 1)
    fig.update_layout(xaxis_rangeslider_visible = False)
    fig.update_layout(
    yaxis_title = 'Apple Stock Price USD ($)'
    )
    
    
    fig.add_trace(go.Scatter(x = df.index, 
                             y = df['Volume']), 
                             row = 3, col = 1)
    fig.update_layout(
        yaxis_title = 'Volume',
        xaxis_title = 'Date'
    )


    fig.update_layout(title_text="Apple Stock")
    
    fig.update_layout(width=900, height=900)

    return fig

chart_can_vol(df)

【问题讨论】:

    标签: python label subplot plotly-python


    【解决方案1】:

    制作子图时,可以添加subplot_titles 属性。在下面的代码中,我使用了标题“test1”和“test2”。更改轴标签时,可以使用update_xaxesupdate_yaxes,只需确保update_axes 方法和子图的行值和列值相同。

    要更改线条的颜色,您可以在 scatterplot 方法中添加line 属性,并将其设置为等于具有所需颜色的十六进制值的字典。

    附:您应该进行更新,因为 tools.make_subplots 已被弃用。更新后,您可以简单地使用 make_subplots。此外,当您应该使用 pandas-datareader 时,您正在使用 pandas。请参阅导入语句。

    代码:

    import numpy as np
    import pandas as pd
    import pandas_datareader.data as web
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    from plotly import tools
    
    
    stock = 'AAPL'
    df = web.DataReader(stock, data_source='yahoo', start='01-01-2019')
    
    
    
    def chart_can_vol(df):
        subplot_titles=["test1", "test2"]
        rows = 2
        cols = 2
        height = 300 * rows
        
        fig = make_subplots(
            rows=3, cols=1,
            specs=[[{"rowspan": 2}], 
                   [None],
                   [{}]],
            shared_xaxes=True,
            subplot_titles=("test1", "test2"),
            vertical_spacing=0.1)
    
        fig.add_trace(go.Candlestick(x = df.index,
                                    open = df['Open'],
                                    close = df['Close'],
                                    low = df['Low'],
                                    high = df['High']),
                     row = 1, col = 1)
        fig.update_layout(xaxis_rangeslider_visible = False)
        fig.update_layout(
        yaxis_title = 'Apple Stock Price USD ($)'
        )
        
        
        fig.add_trace(go.Scatter(x = df.index, 
                                 y = df['Volume'],
                                 line= dict(color="#ffe476")),
                                 row = 3, col = 1)
    
        fig.update_xaxes(title_text="Date", row = 3, col = 1)
        fig.update_yaxes(title_text="Volume", row = 3, col = 1)
    
    
        fig.update_layout(title_text="Apple Stock")
        
        fig.update_layout(width=900, height=900)
    
        return fig
    
    chart_can_vol(df).show()
    
    

    【讨论】:

    • 谢谢。我可以更改线条颜色,但仍然只有一个 yaxis 标签,仅用于上图。通过添加 yaxis_title = 'Volume',它会覆盖 yaxis_title = 'Apple Stock Price USD ($)'
    • @sam_sam 更新了我的答案。检查它是否有效
    • 完美,谢谢。 fig.update_xaxes 是关键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    相关资源
    最近更新 更多