Data and Layout Attributes

Get familiar with the basics of the data and layout attributes.

Having created the basic object, we are now ready to start adding our first data traces to our first chart.

Getting to know the data attribute

First, we start by adding a scatterplot using a very small and simple dataset. Later in the chapter, we’ll use our poverty dataset to create other plots. Once we have created our Figure object and assigned it to a variable, we have access to a large number of convenient methods to manipulate that object. The methods related to adding data traces all start with add_, followed by the type of chart we’re adding, for example, add_scatter or add_bar.

Create a scatterplot

Let’s go through the full process of creating a scatterplot.

Press + to interact
import plotly.graph_objects as go
fig = go.Figure()
fig.add_scatter(x=[1, 2, 3], y=[4, 2, 3])
fig.show()
  • Line 1: We import the graph_objects module.
  • Line 2: We create an instance of a Figure object and assign it to a variable.
  • Line 3: We add a scatter trace. The minimum parameters required for this type of chart are two arrays for the x and y values. These can be provided as lists, tuples, NumPy arrays, or pandas Series.
  • Line 4: We display the resulting figure. We can simply have the variable on the last line in our code cell, and it will also be displayed in JupyterLab once we run it. We can also explicitly call the show method,
...