Plot boarder overlaps legend

I have a plot where the plot boarder is outside the x-axis limits and overlaps the legend.

Here is my code. There is no problem with the data to be plotted.

slope6 = LSslope(0.5)
slope12 = LSslope(0.707)
x6,y6 = slope6.curve(-10,10,201)
x12,y12 = slope12.curve(-10,10,201)

fig = go.Figure()
trace6 = go.Scatter(x=x6,y=y6,mode='lines',line=dict(color='red'),
                    name='$Q = 0.5$')
trace12 = go.Scatter(x=x12,y=y12,mode='lines',line=dict(color='blue'),
                    name='$Q = 0.707$')

fig.add_traces([trace6,trace12])
fig.update_layout(xaxis_title=r'$\text{Filter Gain in }dB$',
                  yaxis_title=r'$\text{Filter Slope in }dB/Octave$',
                  xaxis=dict(ticklen=10),yaxis=dict(ticklen=10),
                  plot_bgcolor='white')
fig.update_xaxes(ticks='inside',mirror=True,showline=True,
                 linecolor='black',gridcolor='lightgrey',zeroline=True,
                 zerolinecolor='lightgrey')
fig.update_yaxes(ticks='inside',mirror=True,showline=True,
                 linecolor='black',gridcolor='lightgrey',zeroline=True,
                 zerolinecolor='lightgrey')
fig.update_layout(title='Maximum Slope of Low Shelf Filter',
                  title_x=0.5,title_font_size=30)
fig.add_shape(type="rect",xref="paper",yref="paper",x0=0,y0=0,x1=1,y1=1,
              line=dict(color="Black", width=2))
pio.write_image(fig,'test.jpg',format='jpeg',width=800,height=600,
                scale=2)
os.system('xdg-open test.jpg')

Hey @brombo welcome to the forums.

Try changing the x- reference for the legend:

fig.update_layout(legend_xref="container")
import plotly.graph_objects as go

x6,y6 = [1,2], [1,1]
x12,y12 = [1, 2], [2, 2]

fig = go.Figure()
trace6 = go.Scatter(x=x6,y=y6,mode='lines',line=dict(color='red'),
                    name='$Q = 0.5$')
trace12 = go.Scatter(x=x12,y=y12,mode='lines',line=dict(color='blue'),
                    name='$Q = 0.707$')

fig.add_traces([trace6,trace12])
fig.update_layout(xaxis_title=r'$\text{Filter Gain in }dB$',
                  yaxis_title=r'$\text{Filter Slope in }dB/Octave$',
                  xaxis=dict(ticklen=10),yaxis=dict(ticklen=10),
                  plot_bgcolor='white')
fig.update_xaxes(ticks='inside',mirror=True,showline=True,
                 linecolor='black',gridcolor='lightgrey',zeroline=True,
                 zerolinecolor='lightgrey')
fig.update_yaxes(ticks='inside',mirror=True,showline=True,
                 linecolor='black',gridcolor='lightgrey',zeroline=True,
                 zerolinecolor='lightgrey')
fig.update_layout(title='Maximum Slope of Low Shelf Filter',
                  title_x=0.5,title_font_size=30)
fig.add_shape(type="rect",xref="paper",yref="paper",x0=0,y0=0,x1=1,y1=1,
              line=dict(color="Black", width=2))
fig.update_layout(legend_xref="container")

Thank you.

1 Like