In Plotly, a trace is a set of data points that are represented in a specific way. For example, in a line chart, a trace would be a single line. Each trace has properties such as the data values, the type of chart (e.g., line, bar, scatter), and the appearance (color, marker style, etc.).
The layout of a Plotly figure defines the overall appearance of the visualization, including the title, axis labels, legend, and background color. It also controls the size and aspect ratio of the plot.
A figure in Plotly is a combination of traces and a layout. It represents the complete visualization that will be displayed.
To use Plotly in Python, you first need to install it. You can install Plotly using pip
:
pip install plotly
If you want to use Plotly in a Jupyter Notebook, you also need to install ipywidgets
:
pip install ipywidgets
And enable the extension:
jupyter nbextension enable --py widgetsnbextension
Let’s start by creating a simple line chart using Plotly.
import plotly.graph_objects as go
import numpy as np
# Generate some sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create a trace
trace = go.Scatter(x=x, y=y, mode='lines', name='Sin(x)')
# Create a layout
layout = go.Layout(title='Simple Line Chart',
xaxis_title='X',
yaxis_title='Y')
# Create a figure
fig = go.Figure(data=[trace], layout=layout)
# Show the figure
fig.show()
In this code, we first generate some sample data for the x and y axes. Then we create a scatter trace with the go.Scatter
class, specifying the x and y values, the mode (lines in this case), and a name for the trace. Next, we create a layout with a title and axis labels. Finally, we combine the trace and the layout into a figure and display it using the show
method.
One of the main advantages of Plotly is its interactivity. For example, you can add tooltips to data points.
import plotly.graph_objects as go
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create a trace with hover text
hover_text = [f'x: {x_val:.2f}, y: {y_val:.2f}' for x_val, y_val in zip(x, y)]
trace = go.Scatter(x=x, y=y, mode='lines', name='Sin(x)', hovertext=hover_text)
layout = go.Layout(title='Line Chart with Tooltips',
xaxis_title='X',
yaxis_title='Y')
fig = go.Figure(data=[trace], layout=layout)
fig.show()
In this code, we create a list of hover text for each data point. Then we pass this list to the hovertext
parameter of the go.Scatter
class. Now, when you hover over a data point, you will see the corresponding x and y values.
You can display multiple traces in a single plot to compare different data series.
import plotly.graph_objects as go
import numpy as np
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
trace1 = go.Scatter(x=x, y=y1, mode='lines', name='Sin(x)')
trace2 = go.Scatter(x=x, y=y2, mode='lines', name='Cos(x)')
layout = go.Layout(title='Multiple Traces in a Single Plot',
xaxis_title='X',
yaxis_title='Y')
fig = go.Figure(data=[trace1, trace2], layout=layout)
fig.show()
In this code, we create two traces for the sine and cosine functions. Then we pass both traces as a list to the data
parameter of the go.Figure
class.
Plotly allows you to create subplots to display multiple visualizations in a single figure.
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import numpy as np
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create subplots
fig = make_subplots(rows=2, cols=1)
# Add traces to subplots
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', name='Sin(x)'), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', name='Cos(x)'), row=2, col=1)
# Update layout
fig.update_layout(title_text='Subplots Example')
fig.show()
In this code, we use the make_subplots
function to create a figure with two rows and one column of subplots. Then we add each trace to the appropriate subplot using the add_trace
method.
Plotly is a powerful and versatile library for creating interactive visualizations in Python. By understanding the fundamental concepts of traces, layouts, and figures, and by following the usage methods, common practices, and best practices outlined in this blog, you can create high - quality visualizations that effectively communicate your data insights. Whether you are a data scientist, analyst, or developer, Plotly can help you take your data visualizations to the next level.