Anindya Banerjee
Anindya Banerjee

Reputation: 41

Plotly: How to add space on right side of plot

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

Upvotes: 4

Views: 1681

Answers (3)

Jack
Jack

Reputation: 1

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

Upvotes: 0

grievesy
grievesy

Reputation: 53

Update your layout..

fig.update_layout(
    autosize=False,
    width=500,
    height=500,
    margin=dict(
        l=50,
        r=50,
        b=100,
        t=100,
        pad=4
    )

See https://plotly.com/python/setting-graph-size/

Upvotes: 0

Derek O
Derek O

Reputation: 19545

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

Upvotes: 1

Related Questions