Intro to Data Visualization
  • Introduction
  • Getting started
    • Introduction to Pandas
    • Accessing Files on Colab
    • Reviewing Data
      • Understanding type(data) in Pandas
    • Data Types
      • Categorical Data
      • Numeric Data
      • Temporal Data
      • Geographic Data
    • How to Check Data Type
    • Slicing and Subsetting DataFrames
    • Aggregating Data
  • Visualization Types
    • Exploratory Process
    • Explanatory Process
  • data exploration
    • Exploration Overview
    • Exploration with Plotly
      • Exploring Distributions
      • Exploring Relationships
      • Exploring with Regression Plots
      • Exploring Correlations
      • Exploring Categories
      • Exploring Time Series
      • Exploring Stocks with Candlestick
      • Exploring with Facets
      • Exploring with Subplots
    • Exploring with AI
  • Data Explanation
    • Data Explanation with Plotly
      • Using Text
      • Using Annotations
      • Using Color
      • Using Shape
      • Accessibility
      • Using Animations
    • Use Cases
  • Exercises and examples
    • Stock Market
      • Loading Yahoo! Finance Data
      • Use Cases for YF
      • Exploring YF Data
      • Understanding Boeing Data Over Time
      • Polishing the visualization
      • Analyzing with AI
      • Comparisons
    • The Gapminder Dataset
      • Loading the Gapminder Data
      • Use Cases
      • Exploring the Data
      • Exporting a Static Image
Powered by GitBook
On this page
  1. Data Explanation
  2. Data Explanation with Plotly

Using Text

Adding titles and captions to a Plotly visualization enhances its clarity and provides context to the audience. In Plotly, you can customize the main title, axis titles, and even add annotations to serve as captions or additional descriptions.

Adding Titles and Captions in Plotly

Example: Adding a Main Title and Axis Titles

import plotly.express as px

# Sample data
data = {
    'Category': ['A', 'B', 'C', 'D'],
    'Values': [10, 20, 15, 25]
}

df = pd.DataFrame(data)

# Create a bar chart with titles
fig = px.bar(df, x='Category', y='Values', title='Main Title: Category vs. Values')
fig.update_layout(
    xaxis_title='Categories',  # X-axis title
    yaxis_title='Values',      # Y-axis title
    title_font=dict(size=24),  # Customize title font size
)
fig.show()

Example: Adding Annotations

In Plotly Express, you can add annotations by updating the layout of the figure with fig.update_layout() and using the annotations parameter. This allows you to place text or symbols on the chart to provide additional information or context.

import pandas as pd
import plotly.express as px

# Sample data
data = {
    'Category': ['A', 'B', 'C', 'D'],
    'Values': [10, 20, 15, 25]
}

df = pd.DataFrame(data)

# Create a bar chart
fig = px.bar(df, x='Category', y='Values', title='Bar Chart with Annotations')

# Add annotations
fig.update_layout(
    annotations=[
        dict(
            x='B',  # X-coordinate of the annotation
            y=20,   # Y-coordinate of the annotation
            text='Highest Value',  # Text of the annotation
            showarrow=True,  # Show an arrow pointing to the coordinate
            arrowhead=2,  # Arrow style
            ax=0, ay=-40,  # Offset for the arrow
            font=dict(size=12, color='black'),  # Font size and color
            align='center'
        ),
        dict(
            x='D',
            y=25,
            text='Second Highest',
            showarrow=False,  # No arrow for this annotation
            font=dict(size=12, color='blue')
        )
    ]
)

# Show the plot
fig.show()

Key Parameters in annotations:

  • x and y: The coordinates where the annotation appears.

  • text: The text content of the annotation.

  • showarrow: Whether to display an arrow pointing to the annotation.

  • arrowhead: The style of the arrow (if showarrow=True).

  • ax and ay: The offsets for the arrow relative to the annotation text.

  • font: Dictionary specifying the font size, color, and style.

  • align: Text alignment, such as 'center', 'left', or 'right'.

Notes:

  • The x and y values in annotations align with the data values in the chart.

  • You can combine multiple annotations in a list to annotate different parts of the chart.

PreviousData Explanation with PlotlyNextUsing Annotations

Last updated 3 months ago

For more information, go to the Plotly documentation: . More details on specific formats are here: .

https://plotly.com/python/tick-formatting/
https://github.com/d3/d3-format/blob/master/README.md#locale_format