Python:Plotly .Figure()

tguajardo's avatar
Published Oct 2, 2024
Contribute to Docs

The .Figure() class in Plotly is used to create and manipulate figures. It provides a high-level interface for creating complex visualizations.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

plotly.graph_objects.Figure(data=None, layout=None, frames=None, skip_invalid=False, **kwargs)
  • data: List of trace objects defining the visual data (e.g., go.Scatter).
  • layout: Layout object or dictionary for customizing figure appearance (e.g., titles, axes).
  • frames: List of frame objects used for creating animations within the figure.
  • skip_invalid: Boolean to skip invalid properties without raising errors.
  • **kwargs: Additional keyword arguments for further customization of the figure.

Example

The following example showcases the use of .Figure():

import plotly.graph_objects as go
years = [2019, 2020, 2021, 2022, 2023]
inflation_rates = [2.21, 1.94, 3.47, 7.97, 5.41]
data = [go.Scatter(x=years, y=inflation_rates, mode='lines+markers', name='Inflation Rate')]
layout = go.Layout(
title='Global Inflation Over the Last 5 Years',
xaxis_title='Year',
yaxis_title='Inflation Rate (%)',
template='plotly_dark'
)
frames = [go.Frame(data=[go.Scatter(x=years[:k+1], y=inflation_rates[:k+1])]) for k in range(len(years))]
fig = go.Figure(data=data, layout=layout, frames=frames)
fig.show()

This example creates a line graph representing global inflation rates over the past five years. It defines data for the plot, layout for appearance settings, and frames for animation steps before passing them to the Figure() class to generate the visualization.

The above code generates the following code

Example for Figure() on Plotly output

All contributors

Contribute to Docs

Learn Python:Plotly on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours