Plotly- Express-17 – Legend and Title Setting (2)

This article introduces the skills of Plotly for graphic legend setting, mainly including:

  • Overall basic Settings
  • Change the legend name
  • Hide Legend entry (first legend)
  • Legend location display
  • Custom beautiful legend
  • Legend scatter size setting
  • Group legend Settings
  • The title is set

reference

https://plotly.com/python/figure-labels/

https://plotly.com/python/legend/

https://plotly.com/python/reference/#layout

Whole set

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[0.1.2.3.4.5.6.7.8].    y=[0.1.2.3.4.5.6.7.8]. name="Name of Trace 1" # Name of the first legend ))  fig.add_trace(go.Scatter(  x=[0.1.2.3.4.5.6.7.8]. y=[1.0.3.2.5.4.7.6.8]. name="Name of Trace 2".# Second legend name  visible='legendonly' # Turn the 2nd legend into grey and click visible  ))  fig.update_layout(  title="Plot Title".# main title  xaxis_title="x Axis Title".Headings for # 2 axes  yaxis_title="y Axis Title". font=dict(  family="Courier New, monospace". size=18. color="#7f7f7f"  ) ) fig.update_layout(showlegend=False.Hide legend. Default is True  legend_title_text='Trend' Change the name of the legend  ) fig.show() Copy the code

legend

Hide legend entry

Change the legend name

The legend shows the location

As a legend, the location is in the upper left corner

Custom legend

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1.2.3.4.5],y=[1.2.3.4.5],))
fig.add_trace(go.Scatter(x=[1.2.3.4.5],y=[5.4.3.2.1],))  fig.update_layout(  legend=dict(x=0,y=1.Location of legend: Think of the axes as unit 1  traceorder="normal". font=dict(  family="sans-serif". size=12. color="black"),  bgcolor="LightSteelBlue".# Background color, border color and width  bordercolor="Black". borderwidth=2  ) )  fig.show() Copy the code

Scatter the size

Grouped Legend

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
 x=[1.2.3]. y=[2.1.3]. legendgroup="group".# this can be any string, not just "group"  name="first legend group".# the name  mode="markers".Markers: markers, lines  marker=dict(color="Crimson", size=10) # mode Settings ))  fig.add_trace(go.Scatter(  x=[1.2.3]. y=[2.2.2]. legendgroup="group". name="first legend group - average". mode="lines". line=dict(color="Crimson") ))  fig.add_trace(go.Scatter(  x=[1.2.3]. y=[4.9.2]. legendgroup="group2". name="second legend group". mode="markers". marker=dict(color="MediumPurple", size=10) ))  fig.add_trace(go.Scatter(  x=[1.2.3]. y=[5.5.5]. legendgroup="group2". name="second legend group - average". mode="lines". line=dict(color="MediumPurple") ))  fig.show() Copy the code

-align Plot Title

import plotly.graph_objects as go

fig = go.Figure(go.Scatter(
    y=[3.1.4].    x=["Mon"."Tue"."Wed"]))
 fig.update_layout(  title={  'text': "Plot Title".# Title name  'y':0.9.Position #, the length of the axis is 1  'x':0.5. 'xanchor': 'center'.# Relative position  'yanchor': 'top'})  fig.show() Copy the code

🏆 technology project phase iii | data visualization of those things…