plotly yaxis2手动缩放

问题描述 投票:1回答:1

我有一个仪表盘仪表板,我似乎无法重新调整我的辅助y轴。有办法吗?我试图弄乱go.Layout中的domain参数和range参数。我需要将体积条形图按比例缩小并占据图表高度的10%,以便它不与我的烛台重叠。

非常感谢。任何帮助表示赞赏。

candlestick_chart

import pandas as pd
import pandas_datareader.data as web
import plotly.offline as pyo
import plotly.graph_objs as go


stock_ticker='AAPL'
start_date='2019-04-01'
end_date='2019-05-22'


data=[]

hist_stock_df = web.DataReader(stock_ticker,'iex',start_date, end_date)



data.append(go.Candlestick(x=hist_stock_df.index,
                            open=hist_stock_df['open'],
                            high=hist_stock_df['high'],
                            low=hist_stock_df['low'],
                            close=hist_stock_df['close'],
                            name='AAPL'))

data.append(go.Bar(x=hist_stock_df.index,
                    y=hist_stock_df['volume'].values,
                    yaxis='y2'))
                    #y0=1000000

layout=go.Layout(title= 'Candestick Chart of AAPL',
                xaxis=dict(title='Date',rangeslider=dict(visible=False)),
                yaxis=dict(title='Price'),
                plot_bgcolor='#9b9b9b',
                paper_bgcolor='#9b9b9b',
                font=dict(color='#c4c4c4'),
                yaxis2=dict(title='Volume',
                            overlaying='y',
                            side='right'))
                            #scaleanchor='y'))
                            #scaleratio=0.00000001,
                            #rangemode='tozero',
                            #constraintoward='bottom',
                            #domain=[0,0.1]))


fig = go.Figure(data=data, layout=layout)

pyo.iplot(fig)

我尝试弄乱注释的参数

UPDATE

通过布局参数的这种组合,我设法重新调整了条形图的比例,但是现在有两个x轴,试图找出如何将中间的x轴向下移动。

Updated paramenters

layout=go.Layout(title= 'Candestick Chart of AAPL',
                xaxis=dict(title='Date',rangeslider=dict(visible=False)),
                yaxis=dict(title='Price'),
                plot_bgcolor='#9b9b9b',
                paper_bgcolor='#9b9b9b',
                font=dict(color='#c4c4c4'),
                yaxis2=dict(title='Volume',
                            overlaying='y',
                            side='right',
                            scaleanchor='y',
                            scaleratio=0.0000001))
plotly scale axis plotly-dash
1个回答
0
投票

使用secondary_y=Boolean中的fig.update_yaxes()指定要调整的轴。

图1: 手动调整

enter image description here

图2: 手动调整

enter image description here

代码:

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

# data
np.random.seed(1234)
numdays=20
dates = pd.date_range('1/1/2020', periods=numdays)
A = (np.random.randint(low=-10, high=10, size=numdays).cumsum()+100).tolist()
B = (np.random.randint(low=0, high=100, size=numdays).tolist())
df = pd.DataFrame({'A': A,'B':B}, index=dates)

# plotly figure setup
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(name='A', x=df.index, y=df['A'].values))
fig.add_trace(go.Bar(name='B', x=df.index, y=df['B'].values), secondary_y=True)

# plotly manual axis adjustments
fig.update_yaxes(range=[50,160], secondary_y=False)
fig.update_yaxes(range=[-10,200], secondary_y=True)

fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.