简体   繁体   English

如何为 plotly 中的子图添加标签?

[英]How to add labels to subplots in plotly?

I am trying to plot a candlestick with volume, using the plotly.我正在尝试使用 plotly 来 plot 一个有量的烛台。 However I can not get the proper x and yaxis label.please help.I need y labels for both plot but xlabel for just the bottom one, also one title for both.但是,我无法获得正确的 x 和 yaxis label。请帮助。我需要两个 plot 的 y 标签,但只有底部一个的 xlabel,也是两者的一个标题。 Bellow is the code.下面是代码。

** one more question, how can I change the line color in the volume plot.Thank you ** 再问一个问题,如何更改卷 plot 中的线条颜色。谢谢

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)

When you make your subplots, you can add the subplot_titles attribute.制作子图时,可以添加subplot_titles属性。 In the code below, I used the titles "test1" and "test2".在下面的代码中,我使用了标题“test1”和“test2”。 When you change your axis labels, you can use update_xaxes and update_yaxes , just make sure that the row and column values are the same for the update_axes method and the subplot.更改轴标签时,可以使用update_xaxesupdate_yaxes ,只需确保update_axes方法和子图的行值和列值相同。

To change the color of the line, you can add the line attribute within the scatterplot method and set it equal to a dictionary with a hex value of the color you want.要更改线条的颜色,您可以在 scatterplot 方法中添加line属性,并将其设置为具有所需颜色的十六进制值的字典。

PS You should update plotly, because the tools.make_subplots was deprecated. PS 你应该更新 plotly,因为tools.make_subplots已被弃用。 Once you update, you can simply use make_subplots.更新后,您可以简单地使用 make_subplots。 Also, you are using pandas, when you should use pandas-datareader.此外,当您应该使用 pandas-datareader 时,您正在使用 pandas。 See import statements.请参阅导入语句。

Code:代码:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM