Search code examples
pythonplotlyplotly-python

Plotly Express: How can I adjust the size of marginal distribution plots?


I have several datasets I am plotting with plotly express's scatterplot. I am using the option of the marginal distribution plots to show a histogram of the datasets but the problem is that the histograms on the margins tend to be way too big, eclipsing the actual data. An example:enter image description here

In these plots it's important to see the individual points so I would like to scale the histograms down, but I don't find any documentation on how to adjust the size of the marginal distribution plots separately.

How could I do this?


Solution

  • Since your code has not been posted, I will answer assuming that you have enabled marginal in plotly.express. express has a fixed size for individual graphs, I assume, so you can check the graph configuration information (fig.layout) for each graph size (domain ) in the graph configuration information (fig.layout) and set a new value. The answer code is taken from the reference.

    import plotly.express as px
    
    df = px.data.iris()
    
    fig = px.scatter(df,
                     x="sepal_length",
                     y="sepal_width",
                     marginal_x="histogram",
                     marginal_y="rug")
    
    # x1=0.8358,x2=0.8408
    x1_, x2_ = 0.9, 0.905
    # y1=0.7326, y2=0.7426
    y1_, y2_ = 0.8, 0.81
    fig.layout.xaxis.domain = (0.0, x1_)
    fig.layout.xaxis2.domain = (x2_, 1.0)
    fig.layout.xaxis3.domain = (0.0, x1_)
    fig.layout.xaxis4.domain = (x2_, 1.0)
    fig.layout.yaxis.domain = (0.0, y1_)
    fig.layout.yaxis2.domain = (0.0, y1_)
    fig.layout.yaxis3.domain = (y2_, 1.0)
    fig.layout.yaxis4.domain = (y2_, 1.0)
    
    fig.show()
    

    enter image description here

    Graph before customization enter image description here