如何在情节中为子图添加标签?

时间:2021-01-08 14:33:31

标签: python label subplot plotly-python

我正在尝试使用 plotly 绘制带有成交量的烛台。但是,我无法获得正确的 x 和 yaxis 标签。请帮助。我需要两个图的 y 标签,但底部的 xlabel 需要一个标签,两者都有一个标题。下面是代码。

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

public String toHexString(byte... bytes) {

    return Optional.ofNullable(bytes)
            .filter(bs->bs.length>0)
            .map(DatatypeConverter::printHexBinary)
            .map(str->IntStream.range(0, str.length())
                    .filter(i->(i%2)==0)        // take every second index
                    .mapToObj(i->"0x" + str.substring(i, i+2))
                    .collect(Collectors.joining(" ")))
            .orElse("");
}

1 个答案:

答案 0 :(得分:0)

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

要更改线条的颜色,您可以在散点图方法中添加 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()

相关问题