Lesson

Legends and annotations

Learn Legends and annotations in SQLPad's Data Science in Action course with practical examples and guided lessons.

In this lesson, we will learn how to customize legends and add annotations to our Plotly charts. Legends and annotations are essential to make your charts more informative and easier to understand for the audience.

Customizing Legends

Plotly automatically generates legends for your plots based on the trace names. However, you can customize the appearance and position of the legends to make them more informative and visually appealing.

Let's start by customizing the legend of a simple bar chart using the built-in px.data.gapminder dataset from Plotly.

import plotly.express as px

data = px.data.gapminder().query("year == 2007")
fig = px.bar(data, x='continent', y='pop', color='continent', text='country')

# Customizing legend
fig.update_layout(
    legend=dict(
        title='Continent',
        x=1,
        y=1,
        traceorder='normal',
        font=dict(size=12, color='black'),
        bgcolor='rgba(255, 255, 255, 0.5)',
        bordercolor='black',
        borderwidth=1
    )
)

fig.show()

In the example above, we used the update_layout method to customize the legend. We set the legend title, position (x and y coordinates), trace order, font size and color, background color, and border color and width.

Adding Annotations

Annotations are useful to add text or shapes to specific points or regions in your chart to provide additional information or highlight certain data points.

Let's create a line chart using the built-in px.data.stocks dataset from Plotly and add some annotations to it.

import plotly.graph_objs as go

data = px.data.stocks()

fig = go.Figure()

# Adding traces
fig.add_trace(go.Scatter(x=data['date'], y=data['AAPL'], name='AAPL'))
fig.add_trace(go.Scatter(x=data['date'], y=data['AMZN'], name='AMZN'))
fig.add_trace(go.Scatter(x=data['date'], y=data['GOOG'], name='GOOG'))

# Customizing layout
fig.update_layout(
    title='Stock Prices',
    xaxis_title='Date',
    yaxis_title='Price',
    legend=dict(x=1, y=0.5)
)

# Adding annotations
fig.add_annotation(
    x='2019-12-30',
    y=1.69,
    text='AAPL High',
    showarrow=True,
    arrowhead=1,
    font=dict(size=12, color='black')
)

fig.add_annotation(
    x='2018-12-31',
    y=0.86,
    text='AAPL Low',
    showarrow=True,
    arrowhead=1,
    font=dict(size=12, color='black')
)

fig.show()

In the example above, we created a line chart with three traces (AAPL, AMZN, and GOOG) and customized the layout. Then, we added two annotations to the chart using the add_annotation method. We specified the x and y coordinates, text, arrow visibility, arrowhead type, and font size and color for each annotation.

Conclusion

In this lesson, we learned how to customize legends and add annotations to our Plotly charts. With these techniques, you can create more informative and visually appealing charts for your data visualizations.

Exercises

1. Legends and Annotations

Instruction

Customize the legend and add annotations to a line chart using the built-in px.data.stocks dataset from Plotly.

My Solution

# Your solution goes here

Hint

  1. Import the necessary libraries.
  2. Load the px.data.stocks dataset.
  3. Create a go.Figure object.
  4. Add traces for AAPL, AMZN, and GOOG using go.Scatter.
  5. Customize the layout using update_layout.
  6. Add annotations using add_annotation with specified x, y coordinates, text, arrow visibility, arrowhead type, and font size and color.
  7. Display the chart using fig.show().

Solution

# Your solution goes here
import plotly.graph_objs as go

data = px.data.stocks()

fig = go.Figure()

# Adding traces
fig.add_trace(go.Scatter(x=data['date'], y=data['AAPL'], name='AAPL'))
fig.add_trace(go.Scatter(x=data['date'], y=data['AMZN'], name='AMZN'))
fig.add_trace(go.Scatter(x=data['date'], y=data['GOOG'], name='GOOG'))

# Customizing layout
fig.update_layout(
    title='Stock Prices',
    xaxis_title='Date',
    yaxis_title='Price',
    legend=dict(x=1, y=0.5)
)

# Adding annotations for AAPL
fig.add_annotation(
    x='2019-12-30',
    y=1.69,
    text='AAPL High',
    showarrow=True,
    arrowhead=1,
    font=dict(size=12, color='black')
)

fig.add_annotation(
    x='2018-12-31',
    y=0.86,
    text='AAPL Low',
    showarrow=True,
    arrowhead=1,
    font=dict(size=12, color='black')
)

# Adding annotations for GOOG
fig.add_annotation(
    x='2019-12-30',
    y=1.22,
    text='GOOG High',
    showarrow=True,
    arrowhead=1,
    font=dict(size=12, color='black')
)

fig.add_annotation(
    x='2018-08-27',
    y=1.65,
    text='AMZN High',
    showarrow=True,
    arrowhead=1,
    font=dict(size=12, color='black')
)

fig.show()