Search code examples
pythongraphplotly

Exclude name from 'plotly.subplots'


Can you please help me to exclude the name that appears in the second subplot on the graph below (`trace 1'):

enter image description here

I want to delete "trace 1". Please find the code that I use below:

Code:

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np

subplot_titles = ['Histogram', 'Boxplot']
rows = 1
columns = 2
xaxis_title="full_sq"
yaxis_title="Value" 

fig = make_subplots(
    rows=rows, 
    cols=columns, 
    subplot_titles=['Histogram', 'Boxplot']
)

trace0 = go.Histogram(
    x=Df["full_sq"]
)

trace1 = go.Box(
    x=Df["full_sq"], 
    marker_color = 'lightseagreen'
)


list_trace = [trace0, trace1]

for i, col in enumerate(subplot_titles):
    r = int(np.ceil(((i+1)/columns)))
    c = i%2+1
    fig.add_trace(list_trace[i], row=r, col=c)
    fig.add_annotation(name = False)
    fig.update_xaxes(title_text=xaxis_title, row=r, col=c)
    fig.update_yaxes(title_text=yaxis_title, row=r, col=c)

fig.update_layout(height = 400, width = 700, showlegend = False)
fig.show()

Solution

  • trace 1 is the tick-label of the box-plot.

    As you are using a for-loop to generate the plots, I decided to create a list called t_labels which will determine which plots will show/hide the tick labels.

    You want to

    • show the tick labels for the first plot,
    • hide the tick labels for the second plot.

    Therefore the t_labels list will look like: t_labels = [True, False]

    We can then use this list to populate the showticklabels parameter in the subplots.

    Code:

    list_trace = [trace0, trace1]
    t_labels = [True, False]
    
    for i, col in enumerate(subplot_titles):
        r = int(np.ceil(((i+1)/columns)))
        c = i%2+1
        fig.add_trace(list_trace[i], row=r, col=c)
        print(r,c,yaxis_title)
        fig.update_xaxes(title_text=xaxis_title, row=r, col=c)
        fig.update_yaxes(title_text=yaxis_title, row=r, col=c, showticklabels=t_labels[i])