Maintain min/max axis control while still filling down to y axis as with fill='tozeroy'

I have a line graph with 2 lines that I want to fill with 2 different colors, which I do with with fill=‘tozeroy’ in the scatter object. When I add this attribute though, the axis automatically adjusts to include 0. Is there some other way to accomplish the axis control without losing the fill behavior?

Edit: Here’s some sample code

import plotly.graph_objs as go
from plotly.subplots import make_subplots

figure = make_subplots(specs=[[{"secondary_y": True}]])

trace1 = go.Scatter(x=[1,2,3,4,5,6,7,8,9],
                    y=[10,11,12,12,11,10,9,9,9],
                    mode='lines',
                    opacity=0.7,
                    name='Line1',
                    textposition='bottom center',
                    line=dict(width=1.25, color='rgb(0, 0, 255)'),
                    fill='tozeroy',
                    fillcolor='rgba(77, 77, 255, 0.5)',
                    connectgaps=False,
                    )

trace2 = go.Scatter(x=[1,2,3,4,5,6,7,8,9],
                    y=[11,13,16,12,9,8,6,6,6],
                    mode='lines',
                    opacity=0.7,
                    name='Line2',
                    textposition='bottom center',
                    line=dict(width=1.5, color='rgb(255, 0, 0)'),
                    fill='tozeroy',
                    fillcolor='rgba(255, 77, 77, 0.7)',
                    connectgaps=False,
                    )

figure.add_trace(trace1)
figure.add_trace(trace2)

figure.update_layout(height=600,
                         title="test",
                         xaxis={"title": "Date",
                                'rangeselector': {'buttons': list(
                                    [{'count': 1, 'label': '1D', 'step': 'day', 'stepmode': 'backward'},
                                     {'count': 7, 'label': '7D', 'step': 'day', 'stepmode': 'backward'},
                                     {'count': 1, 'label': 'MTD', 'step': 'month', 'stepmode': 'todate'},
                                     {'step': 'all'}])},
                                'rangeslider': {'visible': True}, 'type': 'date'},
                         yaxis={"title": "units"})

figure.show()

I want this appearance, but the axis should scale to ~4 rather than 0, just as it does without fill=‘tozeroy’

Hi @jm85, you can set yourself the range of the yaxis if you don’t want it to go to zero. Please see below an example which uses the min and max values of your y data so that you don’t have to set the bounds manually.

import plotly.graph_objs as go
from plotly.subplots import make_subplots
import numpy as np

figure = make_subplots(specs=[[{"secondary_y": True}]])

y1 = np.array([10,11,12,12,11,10,9,9,9])
y2 = np.array([11,13,16,12,9,8,6,6,6])
all_y = np.concatenate((y1, y2))

trace1 = go.Scatter(x=[1,2,3,4,5,6,7,8,9],
                    y=y1,
                    mode='lines',
                    opacity=0.7,
                    name='Line1',
                    textposition='bottom center',
                    line=dict(width=1.25, color='rgb(0, 0, 255)'),
                    fill='tozeroy',
                    fillcolor='rgba(77, 77, 255, 0.5)',
                    connectgaps=False,
                    )

trace2 = go.Scatter(x=[1,2,3,4,5,6,7,8,9],
                    y=y2,
                    mode='lines',
                    opacity=0.7,
                    name='Line2',
                    textposition='bottom center',
                    line=dict(width=1.5, color='rgb(255, 0, 0)'),
                    fill='tozeroy',
                    fillcolor='rgba(255, 77, 77, 0.7)',
                    connectgaps=False,
                    )

figure.add_trace(trace1)
figure.add_trace(trace2)

figure.update_layout(height=600,
                         title="test",
                         xaxis={"title": "Date",
                                'rangeselector': {'buttons': list(
                                    [{'count': 1, 'label': '1D', 'step': 'day', 'stepmode': 'backward'},
                                     {'count': 7, 'label': '7D', 'step': 'day', 'stepmode': 'backward'},
                                     {'count': 1, 'label': 'MTD', 'step': 'month', 'stepmode': 'todate'},
                                     {'step': 'all'}])},
                                'rangeslider': {'visible': True}, 'type': 'date'},
                         yaxis={"title": "units",
                                "range":(0.9 * np.min(all_y), 
                                         1.1 * np.max(all_y))})

figure.show()