Data and Layout Attributes
Explore how to create and enhance Plotly Figure objects by adding data traces like scatterplots and configuring layout attributes such as titles, axis labels, and legends. Understand how to interactively inspect and customize figures using the show method's configuration options, enabling you to build detailed and user-friendly data visualizations.
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.
- Line 1: We import the
graph_objectsmodule. - Line 2: We create an instance of a
Figureobject 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
xandyvalues. These can be provided as lists, tuples, NumPy arrays, or pandasSeries. - 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
showmethod,