How to Create Small Multiple Charts in Python, with Plotly

🧵[1/23]

sharpsightlabs.com/blog/plotly-sm…

#python #datascience #pythoncode #datavisualization
[2/]

Before I get into the mechanics of how to create a small multiple charts in Python, let me quickly explain why they are so important.
[3/]

Small multiple charts are one of my favorite chart types.

They are very powerful, and also highly under-used.

#datascience #dataanalytics #datavisualization
[4/]

The reason I love them so much, is that data analysis is a huge part of data science.

And data analysis is largely about making *comparisons*

#datascience #dataanalytics #datavisualization
[5/]

When we analyze data, we often need to make comparisons between different categories.

Examples:
– Team A vs team B
– Country X vs country Y
– Experiment group α vs experiment group β

#datascience #dataanalytics #data
[6/]

If you need to compare across categories, one great technique is take a data visualization (histogram, scatterplot, bar chart, etc) and make one small version of the chart for every level of a categorical variable.

#datascience #dataanalytics #datavisualization
[7/]

If you create these small plots and organize them into a grid, it usually makes it easy to compare metrics across categories.

#datascience #dataanalytics #datavisualization
[8/]

We have a name for this chart type: the small multiple chart

(AKA: trellis plot, grid plot)

#datascience #dataanalytics #datavisualization
[9/]

Creating small multiples is sometimes complicated ...

Because if you don’t have the right software, then you need to create them manually, which requires you to subset your data down to particular categories, and then create one plot per category.

#datavisualization
[10/]

But with Plotly Express, creating small multiple charts is easy.

#datascience #Python #datavisualization
[11/]

To create small multiple charts with Plotly, you need to use some special parameters for the Plotly Express functions:

– facet_row
– facet_col
– facet_col_wrap

#datascience #dataanalytics #datavisualization #Python #pythoncode
[12/]

These small-multiple parameters are available for several important Plotly Express functions, like:

– px.histogram
– px.scatter
– px.line
– px.imshow

… just to name a few

#datascience #Python #pythoncode
[13/]

So to create a small multiple in Plotly, you call the Plotly Express function

... then use one of the small multiple parameters inside the function call.

#datascience #dataanalytics #datavisualization #Python #pythoncode
[14/]

Let’s take a look at some examples.
[15/]

Let’s say that you have a Plotly histogram:

#datascience #datavisualization #Python #pythoncode
[16/]

You can create a small multiple faceted by column, by using the facet_col= parameter.

#datascience #dataanalytics #datavisualization #Python #pythoncode
[17/]

If you facet by column, and you have too many categories ...

... you can “wrap” the columns with the facet_col_wrap= parameter:

#datascience #dataanalytics #datavisualization #Python #pythoncode
[18/]

To create a small multiple that’s faceted by row, you can use the facet_row= parameter.

#datascience #dataanalytics #datavisualization #Python #pythoncode
[19/]

And if you want to facet by *two* categorical variables – one by column and one by row – you can use facet_row= and facet_col= together:

#datascience #dataanalytics #datavisualization #Python #pythoncode
[20/]

As I said previously, small multiple charts are an extremely powerful technique.

And they are easy to make in Python, if you use Plotly express.

#datascience #dataanalytics #datavisualization #Python
[21/]

If you want to read my full tutorial about small multiples with Plotly, along with the code, you can get it here:

sharpsightlabs.com/blog/plotly-sm…

#datascience #dataanalytics #datavisualization #Python #pythoncode
[22/]

Do you have questions about small multiple charts, Plotly, or data visualization in Python?

Leave a comment and I’ll reply.

#datascience #dataanalytics #datavisualization #Python #pythoncode
[23/]

And if you want to master data science and data visualization, follow me here: @Josh_Ebner

Every day, I post tutorials and threads about data science, data visualization, and machine learning in Python and R.

#datascience #dataanalytics #datavisualization #Python #rstats

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Joshua Ebner

Joshua Ebner Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Josh_Ebner

11 Jan
How to Do a Data Analysis

🧵[1/42]

#datascience #DataAnalytics #Python #rstats
[2/42]

When you do data analysis, you first need to start by clarifying objectives.

Why are you doing the analysis?
What’s the end goal? (e.g., the thing you’re trying to improve, understand, etc)

#datascience #DataAnalytics #data
[3/42]

To do this in a business setting, you’ll typically talk with stakeholders, business partners, and other team members who are familiar with the subject of the analysis.
Read 42 tweets
10 Jan
Why You're Very Likely to Become A Millionaire in Data Science or Machine Learning

🧵[1/n]

#datascience #jobs #money #machinelearning
[2/n]

The reasons that you're likely to become a millionaire in data science:

1. salaries are already high in 2021
2. competition for high salaries is weaker than you think
3. salaries are likely to increase in the 2020s

Let's look at each of these.

#datascience #money #jobs
[3/n]

Let's start with current salaries.

According to Kaggle, the median salary for a US Data Scientist in 2021 is close to $200,000.

kaggle.com/kaggle-survey-…

#datascience #data #jobs
Read 30 tweets
30 Dec 21
Merging two or more datasets is extremely important in data science.

Here's a quick thread that covers the basics of data merges in Python.

🧵[1/19]

#Python #datascience #DataAnalytics
[2/19]

In Python ...

You can combine two Pandas dataframes using the "merge" function.

You can also use the "join" function (which defaults to joining on the index)

#Python #datascience #DataAnalytics
[3/19]

When you merge dataframes, you'll typically have a so-called "key" variable.

This is the variable upon which you'll join the dataframes.

#Python #datascience
Read 19 tweets
29 Dec 21
In Python ...

You can combine Numpy arrays vertically or horizontally using np.concatenate

#Python #pythoncode #datascience
The first argument to the function is a list (or collection) of arrays that you want to combine.

You can actually combine many arrays ...just put them inside the list.
The axis parameter controls the direction along which you combine the arrays.

For 2D arrays ...

'axis = 0' combines vertically
'axis = 1' combines horizontally
Read 6 tweets
29 Dec 21
In Matplotlib ...

You can get the RGBA representation of a color with the to_rgba() function.

#Python #pythoncode #datavisualization
You'll notice that the output of to_rgba is a tuple with four floats: (%red, %green, %blue, alpha)

#Python
In RGBA, the alpha channel represents the opacity of the color, where:

– 0.0 is fully transparent
– 1.0 is fully opaque
Read 4 tweets
28 Dec 21
In Python, you can visualize images with the Plotly IMshow function.

🧵[1/8]

sharpsightlabs.com/blog/plotly-im…

#Python #pythonlearning #datascience #datavisualization
[2/8]

You can use Plotly IMshow for a few uses.

You can use it to plot heatmaps ...

But you can also use it to plot images.
[3/8]

The syntax for Plotly IMshow is pretty simple.

You call the function as px.imshow and then provide the name of the image file you want to visualize.

(This assumes you've imported Plotly express as px)

#Python #pythoncode Image
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(