This article discusses how to create professional-looking charts in Plotly by using custom styling and templates.
Abstract
The article begins by explaining the motivation behind using custom styling in Plotly charts, which is to make them more visually appealing and professional. The author then provides a toy dataset and demonstrates how to create a bar chart using Plotly. The default chart is shown, and the author explains why it looks generic and boring. A custom style template is introduced to address these issues, which includes changing the title and label fonts, applying a different color scheme, and adjusting the background color and grid lines. The author also shares a trick for creating a subtitle in Plotly charts. The final result is a more visually appealing and professional-looking chart. The article concludes by encouraging readers to try out the custom styling and provides a link to a cost-effective AI service.
Bullet points
The author uses custom styling in Plotly charts to make them more visually appealing and professional.
A toy dataset is provided, and a bar chart is created using Plotly.
The default chart is shown, and the author explains why it looks generic and boring.
A custom style template is introduced to address these issues.
The custom style template includes changing the title and label fonts, applying a different color scheme, and adjusting the background color and grid lines.
The author shares a trick for creating a subtitle in Plotly charts.
The final result is a more visually appealing and professional-looking chart.
The article concludes by encouraging readers to try out the custom styling and provides a link to a cost-effective AI service.
A Clean Style for Plotly Charts
The default charts out of the box look great, but I often find myself making a few common changes to make them “pop”.
It wasn’t until recently, while I was building a reporting pipeline did I discover the custom templating function of plotly. Before this, I was producing charts and editing how they looked afterwards. With the custom templating functionality in plotly, you can define a custom style applied as the chart is created. This handy feature reduced my reporting pipeline's complexity and has become a core part of how I use plotly both professionally and at home.
To see how this works, let's create a toy dataset. See below for the required imports and the generation of the dataset to help illustrate how to make plotly charts look more professional.
For this article’s purposes, we will give an example using a bar chart since it is a good choice to encode two categorical variables and a numeric value. We will encode the Animals along the X-axis, the Heights along the Y-axis, and using colour to encode the number of legs.
Image by Patrick Coffey (Plotly)
While the above chart doesn't have anything inherently wrong with it, it just looks a little bit boring and generic. I find the grey coloured background to be a little too dark for my liking and the title and label fonts are just a bit too bland. Let's define a custom style template and apply it to our chart to fix some of those problems.
In the above custom style template, we are changing the title font to Lato because it is a nice readable font, which is also quite compact in terms of its width. This allows titles to be longer without going off the charts' edges and is a simple way to describe better the story the chart is attempting to convey.
Typography is an art I can't wrap my head around; I opt for the trial and error approach!
Nunito is a font I found by this method, and I use it for the axis labels. To me, it looks like a slightly squarer version of Comic Sans, which ironically seems to look quite professional on a chart. If your labels are long, then it might be a good idea to try Lato here since it is a slightly condensed font and will fit more characters in the same width. Let's see what our chart looks like when we apply this styling!
Image by Patrick Coffey (Plotly)
Alright, now we are starting to get somewhere! Apart from the fonts, we have made a few other changes to make the chart a little nicer. We have applied a different colour scheme; I generally opt for the G10 colours scheme built into plotly as a starting point. It seems to be widely accepted by people I produce charts for and works in many different aesthetics. It can be a little bright, but I think this lets our encoded data stand out from the other chart elements. The background colour has been switched to white.
This allows for a larger negative contrast from the encoded data, which again helps it “pop”.
The Y-axis grid lines use a very light shade of grey and are very thin; this allows them to be still seen but not to add too much cognitive load to the reader when interpreting this chart. The labels have received a similar treatment. They have been greyed slightly not to distract the reader from the data, they are still there for reference when the reader needs them, but they don't subconsciously draw the users eyes away from the data until they are ready.
Another common trick I use is to create a subtitle for my charts. This allows me to have a nice short and succinct title while describing important information like the data range or collection method in the subtitle, which adds some nice context to the chart (or sometimes is even mandatory when dataset attribution is required!).
In plotly, we can achieve a subtitle by adding some HTML formatting to the title string.
We will Bold the title, emphasize it, add a line break, then use a span element with a reduced font size to add a subtitle.
Image by Patrick Coffey (Plotly)
And that's it! You can now make professional-looking charts in plotly, using my opinionated custom styling as a starting point. Good luck and happy plotlying!