bluered_earth
bluered_earth

Reputation: 169

Changing title of a python plotly subplot, their aspect ratio, highlighted, bold and different color

I want to highlight, and change the color and font of subplot (Run_2) and subplot (Run_3) in different color and make their font bold as well as drawing a black line square around them to somehow highlight them from the other two subplots. Also, as you can see, their aspect ratio is not square. I could not find any way to change the aspect ratio to square. Is there any way to do this? I have attached a snapshot of what I currently get below.

enter image description here

Here is my code:

    import numpy as np
    from plotly.subplots import make_subplots
    from math import exp

    #fig = make_subplots(2, 2)
    fig = make_subplots(rows=2, cols=2, subplot_titles=("Run_1", "Run_2", "Run_3", "Run_4"), 
                       horizontal_spacing = 0.04, vertical_spacing = 0.09)


    x = np.linspace(0, 10, 1000)
    y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
    fig.add_trace(
        go.Scatter(
            x=x,
            y=y,
            name=f'\N{Greek Small Letter Sigma}(x)',

        ),
        row=1,
        col=1
    )

    x = np.where(np.random.randint(0, 2, 100)==1)[0]
    fig.add_trace(
        go.Scatter(
            x=x,
            y=np.zeros_like(x),
            mode='markers', 
            marker=dict(
                    symbol='circle-open',
                    color='green',
                    size=5
                ),

        ),
        row=1,
        col=2
    )

    x = np.where(np.random.randint(0, 2, 100)==1)[0]
    fig.add_trace(
        go.Scatter(
            x=x,
            y=np.zeros_like(x),
            mode='markers', 
            marker=dict(
                    symbol='circle-open',
                    color='green',
                    size=5
                ),

        ),
        row=2,
        col=1
    )


    x = np.where(np.random.randint(0, 2, 100)==1)[0]
    fig.add_trace(
        go.Scatter(
            x=x,
            y=np.zeros_like(x),
            mode='markers', 
            marker=dict(
                    symbol='circle-open',
                    color='green',
                    size=5
                ),

        ),
        row=2,
        col=2
    )

    fig.show()

Upvotes: 2

Views: 1489

Answers (1)

r-beginners
r-beginners

Reputation: 35155

The aspect ratio is specified by the graph size, so set the height and width. The horizontal and vertical spacing of the graph is adjusted to maintain the aspect ratio. For the title decoration, the method described in the comments can be used, but since the span tag can be used, the font color and thickness are added. Finally, the frame of a particular subplot updates the xy axis settings. Further mirroring it will add settings to both axes of each.

fig = make_subplots(rows=2, cols=2, 
                    subplot_titles=("Run_1", "<span style='font-weight:bold;'>Run_2</span>", "<span style='color:#ff0000;'>Run_3</span>", "Run_4"), 
                    horizontal_spacing = 0.1, vertical_spacing = 0.1)
...
fig.update_layout(autosize=False, height=600, width=600)
fig.update_xaxes(showline=True, linewidth=2, linecolor='black', mirror=True, row=1, col=2)
fig.update_yaxes(showline=True, linewidth=2, linecolor='black', mirror=True, row=1, col=2)
fig.update_xaxes(showline=True, linewidth=2, linecolor='black', mirror=True, row=2, col=1)
fig.update_yaxes(showline=True, linewidth=2, linecolor='black', mirror=True, row=2, col=1)

fig.show()

enter image description here

Upvotes: 2

Related Questions