Sitemap
Alexandre Stamm

Alexandre Stamm

Data Engineer & Enthuasiast. Data Analytics | Data Engineering | Data Science

Data visualization with Python & Plotly

4 min readJul 24, 2022

--

Press enter or click to view image in full size
Photo by Lukas Blazek on Unsplash

How to make beautiful charts & dashboards using Plotly

Hello everyone!

Lately, I’ve been using a lot more Plotly than Seaborn for my Data Visualizations, for a few reasons:

· Plotly charts are interactive, and you can make dashboards easily;

· Some things are much simpler to do in Plotly compared to Seaborn, like putting legend numbers in a line plot;

· Usually when you provide an invalid parameter to a Plotly function/object, it returns a useful error message, with suggestions on what you might have missed;

· Also, I think the default style of Plotly is better than the default style of Seaborn.

It’s not like Seaborn doesn’t have its advantages either, we all know how powerful and complete the Seaborn library is for data visualization. But lately, when I just need to plot some simple and beautiful charts, Plotly has been my go-to library.

That being said, today I’m going to write some general tips & tricks about Plotly, and some examples of what kind of visualizations we can make with this amazing library!

First, if you would like to learn Plotly more in-depth, from how the figure data works, to publishing a Dashboard app using Dash, I suggest reading the documentation, which is very well written and complete.

Now let’s get started!

First, there are two main ways to plot graphs in Plotly, using Plotly Express or Plotly Go (Graph objects).

I mostly use Plotly express when I need to plot simple charts, and use Go when I need more customization options.

Get Alexandre Stamm’s stories in your inbox

Join Medium for free to get updates from this writer.

Plotting charts using Plotly Express is very simple, here are some examples:

Bar charts:

To plot a bar chart with Plotly Express, first, we import a sample dataframe from the library, and then call the px.bar argument:

import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.show()
Press enter or click to view image in full size

Now, say we want to add legend text inside our bars, it’s as simple as adding the text_auto=True argument.

import plotly.express as px
df = px.data.medals_long()

fig = px.bar(df, x="medal", y="count", color="nation", text_auto=True)
fig.show()
Press enter or click to view image in full size

Line charts:

Just like with our bar charts, we’re going to import a sample df and use px.line to plot our chart:

import plotly.express as px

df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()
Press enter or click to view image in full size

Now, to show text legends on our line chart, the code is a little different from the bar chart, but it’s also very simple, we just have to specify where Plotly should get our numbers from (in this case, the “year” column), and it’s also good to use update_traces to specify where we want our text to show:

import plotly.express as px

df = px.data.gapminder().query("country in ['Canada', 'Botswana']")

fig = px.line(df, x="lifeExp", y="gdpPercap", color="country", text="year")
fig.update_traces(textposition="bottom right")
fig.show()
Press enter or click to view image in full size

You can also make all other sorts of charts, like histograms, distplots, boxplots, heatmaps, waterfall… You can find a list of all kinds of charts you can plot here.

Using Plotly Graph Objects for more customizable graphics:

Now let’s try something harder!

Say we want to plot a Bar & Line chart, but using a secondary Y axis for our line chart. Here’s where the customization of Plotly Go comes in hand!

First, let’s create a sample dataframe, with data from visitors in a public park.

Let’s code our chart! I’m going to explain how to do it step-by-step in the code below:

Here’s how our chart looks like:

Press enter or click to view image in full size

And we’re done!

Since we did our chart with Plotly, we can also use Dash to turn it into a dashboard/interactive chart!

There’s a very useful guide on how to deploy your dashboard for free using Heroku that you can find here.

That’s it for today! If you liked this guide, please leave your comment and follow me for more Data Visualization tips/tutorials!

--

--

Written by Alexandre Stamm

Data Engineer & Enthuasiast. Data Analytics | Data Engineering | Data Science