hello, i am using streamlit to plot dashboard and i want to control legend maxheight. i found on plotly documentation we can set maxheight to control legend maxheight, but if i do it in streamlit, it will not work. no matter what height i set , no change will be made.
# Generate sample data with multiple traces (to create a long legend)
np.random.seed(42)
data = {
'x': np.arange(10),
}
# Add 15 different data series to create a long legend
for i in range(15):
data[f'Series {i+1}'] = np.random.randn(10).cumsum()
df = pd.DataFrame(data)
# Create a line plot with Plotly Express
fig = px.line(
df,
x='x',
y=[col for col in df.columns if col != 'x'],
title='Plot with Horizontal Legend (Controlled Max Height)'
)
# Configure the horizontal legend with max height
fig.update_layout(
# Legend configuration
legend=dict(
orientation="h", # Set legend to horizontal
yanchor="bottom", # Anchor point for vertical position
y=1.02, # Position above the plot
xanchor="right", # Anchor point for horizontal position
x=1, # Align to the right
maxheight=0.1, # Set maximum height in pixels
font=dict(size=10), # Adjust font size to fit more items
itemsizing='constant', # Keep marker sizes consistent
bgcolor='rgba(255, 255, 255, 0.8)', # Light background
bordercolor='rgba(0, 0, 0, 0.2)', # Light border
borderwidth=1,
# Enable scrolling if content exceeds maxheight
entrywidthmode='fraction', # Adjust entry width based on available space
entrywidth=0.1 # Fraction of legend width per entry
),
# Adjust plot margins to make space for the legend
margin=dict(t=100) # Top margin to prevent legend cutoff
)
# Display the plot in Streamlit
st.plotly_chart(fig, use_container_width=True)