yaxis2手动缩放比例

时间:2019-05-23 00:11:50

标签: plotly scale axis plotly-dash

我有一个破折号的仪表板,我似乎无法重新缩放我的辅助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)

我尝试弄乱注释的参数

更新

使用这种布局参数组合,我设法重新调整了条形图的比例,但是现在有两个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))

1 个答案:

答案 0 :(得分:1)

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