Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly yaxis2 manual scaling

I have a plotly-dash dashboard and I can't seem to rescale my secondary y-axis. Is there a way of doing this? I've tried messing with the domain parameter and the range parameter in the go.Layout. I need the volume bar chart to be scaled down and occupy maybe 10% of the height of the plot so it doesn't overlap with my candlesticks.

Thank you very much. Any help is appreciated.

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)

I have tried messing with the commented parameters

UPDATE

With this combination of layout parameters I managed to rescale the bars, but now there are two x-axis, been trying to figure out how to bring the middle x-axis down.

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))
like image 234
Omar Omeiri Avatar asked Oct 28 '22 14:10

Omar Omeiri


1 Answers

Use secondary_y=True or secondary_y=False within fig.update_yaxes() to specify which axis to adjust.

Plot 1: Without manual adjustments

enter image description here

Plot 2: With manual adjustments

enter image description here

Code:

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()
like image 74
vestland Avatar answered Nov 15 '22 06:11

vestland