Adjust axis margins

Hello everyone ^^,

As you can see in the image below, there’s a margin on the left side. How can I fix this? The image has also another trace, type scatter.

Screenshot from 2024-09-18 15-21-24

   
    mip_z = np.max(stack, axis=0)
    fig = px.imshow(mip_z, zmin=mip_z.min(), zmax=mip_z.max())
    fig.add_trace(beads)

    fig = fig.update_layout(
        coloraxis={"colorscale": color},
        margin={"l": 0, "r": 0, "t": 0, "b": 0},
    )
    # fig = fig.update_yaxes(automargin=False)
    fig = fig.update_xaxes(automargin=False, range=[0, mip_z.shape[1]])
    fig.update_yaxes(autorange="reversed")

Hey @User5 !
It’s me again :grin:

I guess setting the range to [0, max] should do the trick.
I think the issue is that you set new values to the existing fig instead of updating the existing one, try:

mip_z = np.max(stack, axis=0)
fig = px.imshow(mip_z, zmin=mip_z.min(), zmax=mip_z.max())
fig.add_trace(beads)

fig.update_layout(
    coloraxis={"colorscale": color},
    margin={"l": 0, "r": 0, "t": 0, "b": 0},
)

fig.update_xaxes(automargin=False, range=[0, mip_z.shape[1]])
fig.update_yaxes(autorange="reversed")

Or like I suggested in your other post, try:

fig.update_xaxes(rangemode= "nonnegative")
fig.update_yaxes(autorange="reversed", rangemode= "nonnegative")

Hey @Skiks ,

Thanks again for your replies.

Unfortunately, it didn’t fix the issue. For some reasons, when I remove the trace which is a go.Scatter object. The image is normal but once I add the trace I see these magins.

Screenshot from 2024-09-20 09-52-35

 fig.update_layout(
        coloraxis={"colorscale": color},
        margin={"l": 0, "r": 0, "t": 0, "b": 0},
    )
    # fig = fig.update_yaxes(automargin=False)
    fig.update_xaxes(range=[0, mip_z.shape[1]], rangemode="nonnegative",scaleanchor= "y")
    fig.update_yaxes(rangemode="nonnegative")
    return fig

Hey @User5 !
Weird :thinking:
Try :

fig.update_xaxes(range=[0, mip_z.shape[0]])
fig.update_yaxes(range=[0, mip_z.shape[1]])

# or if you need to reverse the y axis:
fig.update_yaxes(range=[mip_z.shape[1], 0])

Hey @Skiks ,

I tried that before but it didn’t work.

I think go.Scatter adds some margins. I don’t know how to remove them.

@User5 , it seems fine here:

Summary
from dash import dcc, Dash
import plotly.express as px
from skimage import io

app = Dash(__name__)

img = io.imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')

fig = px.imshow(img)

x = [i * 50 for i in range(5)]
fig.add_scatter(mode='markers', x=x, y=x, marker_size=10)

fig.update_xaxes(range=[0, img.shape[0]])
fig.update_yaxes(range=[img.shape[1], 0])

app.layout = dcc.Graph(id='cool-graph', figure=fig)

if __name__ == "__main__":
    app.run_server(debug=True)

{A1DED86D-3BF0-4517-903A-DEF20FC98946}

Compare to no ranges set:
{1583789D-2496-4EAF-B91F-D05318AC2C7A}

In my case, I used add_trace(go.Scatter()).

Let me try using add_scatter