Short guide: Plotting data in Python with Plotly (2024)

A short guide to create attractive, interactive data visualizations using Plotly in Python.
Author
Affiliation
Hermann Hesse

TensorScience

Published

December 20, 2023

Introduction

The ease with which you can create dynamic charts with Plotly in Python - to bring your data to life - I find incredibly useful. I find graphs and so on much easier to edit with Plotly than Matplotlib. The syntax of Plotly is much more intuitive. Below is a short guide on plotting and visualizing with it.

Introduction to Plotly in Python

A simple scatter plot created with plotly showing a clear trend between two variables

As someone who’s been dabbling in data visualization for a while, I find that starting with the right tools can make all the difference. When I first discovered Plotly for Python, what stood out to me was how effortlessly it seemed to balance power with simplicity. Plotly is an open-source graphing library that allows you to create interactive, publication-quality graphs online.

Here’s how you can get started with Plotly in a Python environment. First things first, you’ll need to install it.

!pip install plotly

Once installed, you can import Plotly like any other Python library. However, unlike more traditional options like matplotlib, Plotly’s strength lies in its interactivity and web-friendliness. Here’s a quick example that demonstrates what I mean:

import plotly.express as px

# Let's plot a simple scatter plot
df = px.data.iris()  # Using Iris dataset
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()

Executing this code does not just produce a static image. Instead, you’ll see an interactive plot where you can zoom in, pan, or hover over points to see more information. This feature becomes incredibly handy when dealing with large datasets where spotting trends and outliers can be difficult.

Plotly also integrates beautifully with Pandas, the de facto data manipulation library in Python. If your data is in a DataFrame, plotting it can be as natural as it gets. Here’s an example of plotting a time series from a DataFrame:

import pandas as pd
import plotly.express as px

# Create a simple DataFrame
df = pd.DataFrame({'Date': pd.date_range(start='2024-01-01', periods=30),
'Value': np.random.randn(30).cumsum()})

# Plot a time series chart
fig = px.line(df, x='Date', y='Value', title='Random Time Series')
fig.show()

What’s not to love about the simplicity here? I can tailor a chart with minimal code, focusing more on the story I’m trying to tell with my data, rather than getting bogged down by complex plotting syntax.

It’s important to note that Plotly is not just a single foreheaded beast. The library is expansive, with Plotly Express being a higher-level interface that simplifies the creation of common chart types. Plotly.graph_objects, on the other hand, gives you more control and is typically used for custom or more intricate visualizations.

To give you an idea of how graph_objects works, let’s follow this up with a bar chart:

from plotly.graph_objs import Bar, Layout
from plotly import offline

data = [Bar(x=['A', 'B', 'C'], y=[20, 14, 23])]

layout = Layout(title='Simple Bar Chart',
xaxis=dict(title='Category'),
yaxis=dict(title='Values'))

fig = dict(data=data, layout=layout)
offline.iplot(fig)

Using graph_objects is a tad more verbose, but it opens up a world of customization that can often be necessary for more nuanced visual storytelling.

As someone who appreciates the essence of clean design and interactive visualization, Plotly has become a go-to for my data plotting needs. It’s a tool that grows with your proficiency. Whether you’re just starting out and need to get a feel of your data, or you’re deep into data analysis and need to craft detailed and interactive visual narratives, Plotly offers a suite diverse enough for your journey.

To sum up, getting up and running with Plotly is straightforward, and the payoff is huge. Just a few lines of code can provide insights that might take considerable effort with less interactive tools. And we all know, when it comes to data, being able to comprehend and communicate effectively is half the battle won.

Building Your First Plotly Chart

A colorful bar chart made with plotly to illustrate sales data across different regions

When I first started working with Plotly to visualize data, the simplicity and power of it were striking. Here’s how I built my initial Plotly chart, and hopefully, this will help you breeze through your first Plotly experience.

You need to have Plotly installed, so if you haven’t done that yet, just run:

pip install plotly

Now, onto the fun part. Let’s begin by plotting a simple line chart. We’ll use Plotly’s graph_objs module which offers a straightforward way to create a variety of chart types.

import plotly.graph_objs as go
import plotly.offline as pyo

# Sample data for our line chart
x_data = [0, 1, 2, 3, 4]
y_data = [x ** 2 for x in x_data]

# Create a trace, which is Plotly's term for a series of data points
trace = go.Scatter(
x = x_data,
y = y_data,
mode = 'lines',
name = 'quadratic'
)

# Group the trace into a list - this allows us to add more traces later
data = [trace]

# Define the layout of our chart
layout = go.Layout(
title = 'My First Plotly Chart',
)

# Combine our data and layout into a figure
figure = go.Figure(data=data, layout=layout)

# Display the figure
pyo.plot(figure, filename='first_plotly_chart.html')

The above code sets up a basic line chart plotting the x_data against y squared. Notice that we create a so-called trace object, which in Plotly’s nomenclature represents the set of points we want to plot. After defining the trace, we group it into a list, which will come in handy when you want to plot multiple datasets in the same chart.

To visualize our figure, we tie it together with a layout in go.Figure, and then the pyo.plot() function is called to display the figure in your web browser.

Here’s another quick example - this time a bar chart. The setup is quite similar to the line chart, but we use go.Bar instead.

# Sample data for our bar chart
categories = ['Apples', 'Oranges', 'Pears', 'Grapes']
values = [5, 10, 3, 7]

# Create a trace for the bar chart
trace = go.Bar(
x = categories,
y = values
)

# Again, group the trace into a list
data = [trace]

# Using the same layout as the line chart for consistency
layout = go.Layout(
title = 'My First Plotly Bar Chart',
)

# Create a figure and then display it
figure = go.Figure(data=data, layout=layout)
pyo.plot(figure, filename='first_plotly_bar_chart.html')

In the above snippet, I made a bar chart that shows the quantity of different fruit categories. The process of creating and displaying the figure remains the same; all that changes is the type of trace you’re using.

Practice is crucial, so I’d recommend playing around with the various trace types (go.Scatter, go.Bar, go.Pie, etc.) and getting comfortable with the idea of traces and layouts, as these concepts are fundamental to using Plotly.

There’s a wealth of resources out there. If you hit a snag, the Plotly community forums are a great place to ask questions. The Plotly Python documentation is also an excellent reference with plenty of examples.

That’s about it for your first Plotly chart. In the following sections, you’ll learn how to customize these charts to make them more informative and visually engaging, as well as how to add interactivity to your Plotly plots. Happy plotting!

Customizing Plotly Charts for Visual Appeal

A customized line graph with plotly featuring annotations and styled axes for improved readability

Customizing Plotly charts adds a layer of professionalism and personal touch to data visualization. When I first started using Plotly, the plethora of customization options was overwhelming, but mastering these steps boosted the presentation of my data significantly. Here’s how you can do the same.

import plotly.graph_objs as go

# Create a basic scatter plot
fig = go.Figure(data=[go.Scatter(
x=[1, 2, 3],
y=[4, 1, 2],
mode='markers'
)])

# Customize the layout
fig.update_layout(
title='Customized Scatter Plot',
xaxis_title='X Axis Title',
yaxis_title='Y Axis Title',
font=dict(family='Courier New, monospace', size=18, color='RebeccaPurple')
)

# Customize marker appearance
fig.update_traces(
marker=dict(size=12, color='LightSkyBlue', symbol='circle', line=dict(color='MediumPurple', width=2))
)

fig.show()

In the code example above, I’ve customized the scatter plot’s layout and marker style. By updating the title, and xaxis_title, and yaxis_title, the chart communicates information more clearly. The font dict changes the text style, giving it a distinct appearance. Moreover, update_traces method customizes markers, enhancing visual appeal. To learn more about creating effective data visualizations in Python, see our Short tutorial: visualizing data in Python with Altair.

Color schemes are paramount in making charts visually enticing. Plotly supports various color scales, and you can apply these to bar charts with ease.

import plotly.express as px

# Create a basic bar chart
df = px.data.gapminder().query("country=='Canada'")
fig = px.bar(df, x='year', y='pop')

# Customize colors
fig.update_traces(marker_color='rgba(135, 206, 250, 0.6)',
marker_line_color='rgba(0, 191, 255, 1.0)',
marker_line_width=1.5,
opacity=0.6)
fig.update_layout(title_text='Population Over Years in Canada')
fig.show()

The snippet applies a semi-transparent blue color to the bars and a solid blue outline. This adds depth to the chart and makes it more engaging.

When displaying multiple series of data, it’s helpful to customize the legend to enhance readability. Plotly’s legend can be positioned and styled to fit the context of the data.

import plotly.graph_objs as go

# Create a line chart with three lines
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6],
mode='lines+markers',
name='Series 1'))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 6, 3],
mode='lines+markers',
name='Series 2'))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[6, 5, 4],
mode='lines+markers',
name='Series 3'))

# Customize legend
fig.update_layout(legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.01
))

fig.show()

This code neatly aligns the legend in the top-left corner, unobstructed and clear for interpretation.

Lastly, themes are a convenient shortcut for customization. Plotly offers several built-in themes that instantly modify the chart’s aesthetics.

import plotly.io as pio

# Apply a built-in theme
pio.templates.default = "plotly_dark"

fig.show()

This snippet sets the default theme to “plotly_dark,” which applies a dark background and color set suitable for presentations in darker environments.

Remember, while customization can greatly enhance the appearance of your charts, maintain a balance between style and readability. Data is the star; the visual elements are there to support its story.

For comprehensive documentation on customization, Plotly’s official graphing libraries documentation is an invaluable resource. The Plotly community on GitHub and forums like Stack Overflow are also supportive for troubleshooting specific issues.

Incorporating these customizations into your Plotly charts can make your visual data storytelling as impactful as the insights themselves. Keep experimenting with different styles and layouts; sometimes, simple tweaks can make a world of difference.

Interactivity and Sharing with Plotly

A screenshot depicting an interactive plotly dashboard with hover effects and the sharing menu open

Interactivity and sharing are twin pillars that elevate Plotly from a mere visualization tool to a dynamic platform for data exploration and dissemination. I’ve found that the actionable insights I’ve garnered from Plotly charts are best leveraged when they can be prodded, poked, and pulled apart by curious stakeholders who want to see the story behind the numbers.

Let’s walk through how you can implement interactive features and share your Plotly creations.

Interactive elements in Plotly often revolve around hover tools, sliders, and buttons. Adding a hover tool is as simple as setting hover_data in your chart object. Here’s a quick example using a Scatter plot:

import plotly.express as px

df = px.data.iris()  # Using Iris dataset
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species',
hover_data=['petal_length', 'petal_width'])
fig.show()

Sliders and buttons take interactivity up a notch by enabling viewers to manipulate the data they see. For instance, imagine having a time series data and you want to allow users to sift through years:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

# Let's say df is a DataFrame with columns 'Year', 'Value', and 'Category'
fig = make_subplots()

buttons = []

for year in df['Year'].unique():
year_data = df[df['Year'] == year]
for category in year_data['Category'].unique():
category_data = year_data[year_data['Category'] == category]
fig.add_trace(go.Scatter(x=category_data['Date'], y=category_data['Value'], name=category))

buttons.append(dict(method='update',
label=str(year),
args=[{'visible': [year == y for y in df['Year']]}]))

fig.update_layout(showlegend=True, updatemenus=[{"buttons": buttons, "direction": "down", "showactive": True}])
fig.show()

Now, let’s get to sharing. Interactive charts are great, but they’re even better when shared. You can export your Plotly charts to HTML files or images straight from the library:

fig.write_html('my_interactive_chart.html')
fig.write_image('my_chart.png')

However, hosting these on the web brings them to life. Using Dash by Plotly, you could build a web app to present your charts. Here’s how you’d get a very simple Dash app off the ground:

# Ensure you have dash installed: pip install dash
import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

# Reusing our 'fig' from the previous example
app.layout = html.Div([
dcc.Graph(figure=fig)
])

if __name__ == '__main__':
app.run_server(debug=True)

With this kind of setup, anyone with the URL to your Dash app can access, interact with, and learn from your charts. It has been my experience that this immediate access transforms data analytics from something occasionally dry and insular into a vibrant dialogue.

If you’re doing this in a professional or academic context where you need to reference sources, Plotly’s traceability is a powerful feature. You can add text annotations to link back to sources or even encode metadata within your plots that can be accessed programmatically.

Collaboration is another aspect that’s emphasized by Plotly’s sharing capabilities. With Plotly’s online Chart Studio, charts can be edited by multiple users, enabling a team to iteratively refine their visual storytelling.

For those more interested in the code and contributing or exploring further, the Plotly’s GitHub repository is a goldmine of information and is actively maintained: https://github.com/plotly/plotly.py.

In my journey with data visualization, I’ve found Plotly’s blend of interactivity and ease of sharing not just compelling, but also an essential component in translating complex data narratives into digestible stories. As you incorporate these features into your charts, I’m confident you’ll discern the same intrinsic value and, hopefully, share that excitement with others as you distribute your insights.