Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to add space on right side of plot

Tags:

plotly

I would like to increase the white space between the last data point on the two-line graphs and the secondary y-axis. Right now the endpoints are being squeezed to the extreme end. My graph is shown below.

enter image description here

like image 740
Anindya Banerjee Avatar asked Oct 28 '25 17:10

Anindya Banerjee


2 Answers

You can accomplish this by adding blank spaces to the prefix of each of the y-axis tickmarks on the secondary y-axis. I've done this with some finance timeseries data below.

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(
    x=df['Date'], 
    y=df['AAPL.High'],
    name='AAPL High'),
    secondary_y=False
)
fig.add_trace(go.Scatter(
    x=df['Date'], 
    y=df['AAPL.Volume'],
    name='AAPL Volume'),
    secondary_y=True
)

## add as many spaces as desired
fig.update_yaxes(tickprefix = "         ", secondary_y=True)
fig.show()

enter image description here

like image 118
Derek O Avatar answered Oct 31 '25 11:10

Derek O


The easiest way is to add nan values at the end of your dataframe with next dates index. Something like that work :

last_date = pd.to_datetime(df.index[-1])
for i in range(20):
    last_date = last_date+datetime.timedelta(days=1)
    df.loc[last_date] = np.nan
like image 44
Jack Avatar answered Oct 31 '25 11:10

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!