Mastering Plotly: Let your Visualizations Talk Through Plotly
Elevate Your Data Narratives with Interactive Plotly Magic!
Hello Data Enthusiast! If you haven’t used Plotly before, you are really missing out on visualization wizardry, Plotly shines as a powerful Python library that enables you to create interactive and visually appealing charts and graphs for data visualization with very low code.
Mastering Plotly can make you understand your data much more clearly and most importantly it can take your data storytelling to the next level through its interactiveness! In this article, we’ll delve deep into the world of Plotly, exploring its most important spells ( I mean methods ), features, and capabilities, Assuming that you know the theory of these plots. Here’s a glimpse of what lies ahead.
Table of Contents
- Important Plotly Modules
- Basic Plots
∘ Line Plots
∘ Bar Plots
∘ Scatter Plots
∘ Pie Charts
∘ Histograms - Advanced Plots
∘ Box Plot
∘ Violin Plot
∘ Density Heatmap
∘ 3D Plots
∘ Scatter matrix
∘ Facet Grids
∘ Animation Plots
∘ Conclusion
∘ Announcement #100daysMLDL
Let’s witness the magic of plotly
Important Plotly Modules
Plotly has various modules for different purposes, let’s explore them.
- Plotly.express: It is a high-level API for creating quick and easy visualizations. It is used for creating simple charts with minimal code,
import plotly.express as px
to use Plotly Express. When using this, first you can pass the dataframe, and forx
andy
parameters you can just pass the column name, as it is compatable with pandas dataframes. - plotly.graph_objects: This module is part of Plotly’s core functionality and is used for creating a wide range of charts and graphs,
import plotly.graph_objects as go
to use Plotly graph objects. When using this, we cannot pass the data, we can only pass the series forx
andy
parameters. - plotly.offline: In some cases, you might want to work with Plotly offline, especially if you’re generating visualizations for offline use or within Jupyter notebooks.
Basic Plots
we can create all the basic plots with low code and amazing interactivity such as:
1. Hover information displays details when you mouse over the data points.
2. while zooming and panning enable closer examination of the plot.
3. You can select specific data series or data points by clicking on them in the legend or on the plot itself.
4. You can add custom buttons or controls to the plot that trigger specific actions or updates when clicked, and much more.
Line Plots
- Using Plotly Express for Simple Plots
Let’s take a dataset available in Plotly to explore a few line plots.
# Stock data available in the plotly express module
df_stocks = px.data.stocks()
df_stocks.head()
- Line plot for Facebook stocks
# Exploring Facebook stocks through an interactive line plot
px.line(df_stocks, x='date', y='FB')
By running the single above code line, you will be able to generate such a beautifully interactive plot like this.
- Generating Multiple Line plots by just providing a list
# Generate multiple line plots by just providing a list of series names
px.line(df_stocks, x='date', y=['GOOG','MSFT','AMZN','FB'], title='Google Vs. Microsoft vs. Amazon vs. Facebook')
Now, if you only want to see two or three, you can disable the rest by just clicking on the label name on the right side, this is so cool, right?
- Using Graph Objects for further customizations
To achieve these kinds of things, we need to use the graph_objects, which is why it was said that we need the graph_objects module for more complex charts and plotly_express for simpler charts.
- Create a figure object using
fig=go.Figure()
. - To add a plot to the figure use
fig.add_trace()
and pass the graph object to the figure object. - Use
go.scatter(x,y,mode)
to create graph objects for scatter plots and line plots. - And here we need to use the
mode
parameter to specify the plot style, by default it will be assigned to lines which will give a line plot. And for the Facebook, as we gavemode='lines+markers'
it will have the lines along with markers for the points, easy styling! - For line styling, use the
line
parameter and assign a dictionary with various stylings you need.
# Create a figure object for which we can later add the plots
fig = go.Figure()
# pass the graph objects to the add trace method, assign a series to x and y parameters of graph objects.
fig.add_trace(go.Scatter(x=df_stocks.date, y=df_stocks.AAPL, mode='lines', name='Apple'))
fig.add_trace(go.Scatter(x=df_stocks.date, y=df_stocks.AMZN, mode='lines+markers', name='Amazon'))
# Customizing a particular line
fig.add_trace(go.Scatter(x=df_stocks.date, y=df_stocks.GOOG,
mode='lines+markers', name='Google',
line=dict(color='darkgreen', dash='dot')))
# Further style the figure
fig.update_layout(title='Stock Price Data 2018 - 2020',
# customize axis by using xaxis/yaxis '_' and style
xaxis_title='Price',
# customize various styles axis by passing a dictionary
yaxis=dict(
showgrid=False,
zeroline=False,
showline=False,
showticklabels=False,
),
# customize entire plot style
plot_bgcolor='white')
Bar Plots
Using Plotly Express for simple Bar Plots
Use px.bar(data,x,y)
to create your bar chart. You can add an extra dimension to the bar plot by simply using the parameter, in that case you will get the stacked bar plot, if you want it side by side, you can change it with the parameter barmode='group'
. The below snippets are just images.
- Only the people who worked with Matplotlib would understand how easily we are achieving this plot in Plotly, haha:)
Customized Bar Plots
Instead of directly printing the plotly express object, if we save to figure, then we can make more customizations using the update_trace
and update_layout
methods of the figure.
# Filtering data for countries in 2007 greater than 50000000
population = px.data.gapminder()
filtered_data = population[(population.year==2007) & (population['pop']>50000000)]
# Using colors to distinguish a dimension
fig = px.bar(filtered_data, y='pop', x='country', text='pop', color='country')
# Put bar total value above bars with 2 values of precision
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
# Rotate labels 45 degrees
fig.update_layout(xaxis_tickangle=-45)
- If we hadn’t given the color dimension to be country, all of the bars would have the same color. And by using text parameter we were able to set the total population at the top of the bars.
Scatter Plots
Use px.scatter(data,x,y)
to create simple scatter plots. You can add more dimension using color
and size
parameters. By using the graph object, use go.scatter(data,x,y,mode=’markers’)
to create a scatter plot, and we can even add a numeric variable as a dimension with color
a parameter.
# Use included Iris data set
df_iris = px.data.iris()
# Create a customized scatter
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df_iris.sepal_width, y=df_iris.sepal_length,
mode='markers',
marker_color=df_iris.sepal_width,
text=df_iris.species,
marker_showscale=True
))
fig.update_traces(marker_line_width=2, marker_size=10)
Pie Charts
Use px.Pie(data,values=series_name)
to create a pie chart. For further customizations, you can use graph object pie chart with go.Pie()
and we use the update_traces of the figure object to customize the hover info, text, and pull amount ( which is the same as explode, if you are familiar with seaborn ).
Histograms
Use px.histogram(values)
to create a histogram, and you can stack another histogram on top of that using thecolor
parameter. And here is an amazing with histogram plots, you can use marginal
parameter to add a layer of another plot on the top, such as box, violin, or rug.
Here’s an example of adding a violin plot on top of the stack bar chart.
# Stack histograms based on different column data
df_tips = px.data.tips()
fig = px.histogram(df_tips, x="total_bill", color="sex", marginal='violin')
fig.update_layout(bargap=0.2)
- You can hover over the bar charts to see more details and interact with them.
- Hover on the violin plot to see the quartile details.
- And you can click on the side labels to enable or disable them, so cool, huh!
Advanced Plots
Box Plot
Use px.box(data,x,y)
to create the box plot, you can use the color
parameter to add another dimension for the box plot. And using the points=’all’
parameter will show all the points scattered to the left. On hover, you can see the quartile values.
- You can do further customizations using
go.Box(x,y)
, Using that you can also add mean and standard deviation with the parameterboxmean='sd'
. Remember when using graph object, we won’t be passing the data, but just the series. Points parameters is not used in the graph object box method.
Violin Plot
Use px.violin(data,x,y)
to create violin plots. By just giving y
parameters you can create a violin plot for one numeric variable. But if you want to create violin plots for a numeric variable based on a category column, then you can specify the category column in the x
parameter. And additionaly you can add one more category column dimension using the color parameter. Use box=True
parameter to show the box in the plot.
Density Heatmap
If you need to do an analysis for three variables, you can easily opt for the density heatmaps. Use px.density_heatmap(data,x,y,z)
to create a density heat map.
3D Plots
- To create a 3D Scatter plot, you can use
px.scatter_3d(data,x,y,z,color)
px.scatter_3d(flights, x='year', y='month', z='passengers', color='year', opacity=0.7)
- Similarly, you can also create a 3D Line plot using
px.line_3d(data,x,y,z,color)
Scatter matrix
Scatter matrix is similar to the paiplot from sns, which gives an exhaustive idea of the numeric variables. Use px.scatter_matrix(data)
to display scatter matrix. To differentiate further based on any category, as always you an use the color parameter.
Facet Grids
For all the plotly express plots we have seen so far, we can add the facet grids, by using the parameters face_col
and face_row
parameters.
- Similarly, you can use face_col and face_row for histograms and line plots as well.
Animation Plots
You can add animation to your plotly express plots. The animation_frame
parameter specifies which variable in your dataset should be used to create animation frames. The animation_group
parameter is used to group data points within each animation frame.
For example, if you’re visualizing the movement of objects over time, you might use animation_frame
to specify the time steps and animation_group
to group objects by their IDs or labels.
population = px.data.gapminder()
px.bar(population, x="continent", y="pop", color="continent",
animation_frame="year", range_y=[0,4000000000])
- Click the play button to start the animation and hover to interact. This is so cool and simple, right?
References
Conclusion
Here’s a repo link for the detailed code used in this article. I guess now you would agree how powerful yet simple Plotly is! With Plotly, you’re not limited to static graphs but invited to weave stories and paint beautiful insights.
From the elegance of scatter plots to the drama of animated charts, Plotly lets you enhance your data narrative. So, embrace Plotly, let your data dance, and captivate your audience. It’s not just a tool; it’s your brush to paint the future of data visualization. Try it out and Unleash your creativity with Plotly.
If you are interested in Pandas, Numpy, Matplolib, Seaborn, and Plotly, then look no further, I also have detailed articles on these essential libraries for Data Science, Here’s a curated List.
I will be posting more interesting articles related to Machine learning as well. Subscribe to not miss any exciting data science articles!
Announcement #100daysMLDL
Day 10/100 — Revisiting Plotly.
Here’s a Repo Link to the challenge. I invite you all to join me on this exhilarating journey! Whether you’re a fellow data enthusiast or just curious about the world of machine learning, there’s something here for everyone. Let’s learn, grow, and inspire each other. Thank you for reading, Happy learning, and Have a good day :)