1. Editing plot axes
In this lesson, you will learn how to adjust and format the axes of your plots. Let's get started!
2. Our dataset
For this lesson we have aggregated the penguin data by species, averaging their flipper sizes like so.
The default column names don't look presentation-ready.
3. The default axis titles
Let's create a simple bar chart.
We give the correct column names to avoid an error.
This works but we'll want to change the axes titles to make them presentation-worthy.
4. Editing axis titles
Plotly often offers shortcut methods to format plot elements.
This shortcut method uses the update_xaxes and update_yaxes figure attributes. The title_text argument sets what we want to appear on each axs.
We could instead use the general update_layout method and supply the appropriate dictionary mapping what layout elements to update. Here we set the title element. This is itself a dictionary where we set the text key as above.
As noted before, we will stick with the update_layout method for consistency.
5. Cleaning up our plot
Both these methods produce a plot with more professional axes titles.
6. Which method to use?
So which method should we use? The shortcut method is great when you want to make a quick, simple change.
The update_layout method allows much more extensive styling of the axes
including the font family, font size, text angle, text color and much much more.
You can view all formatting options on the plotly reference documentation.
7. Editing axes ranges
Sometimes you may want to force Plotly to use a specific range for your axes, rather than an automatic range.
On our plot, we couldn't really see the difference between the categories since the values were so similar. To overcome this, let's set the y-axis to start at 150 and end after the maximum of all values (with a small buffer).
We use the general update_layout method and edit the yaxis argument, which has a range argument. This is a list of two values, the min and max of the range.
8. Our new axes ranges
We now get our bar chart with specific axes. Nice stuff.
9. Data scale issues
Sometimes, instead of being very close, you may want to visualize data where values of the categories are very different.
Let's use this recent data of the top 10 countries by the number of billionaires.
You can already see there is a huge gap between the top and bottom entries,
10. Our scale problem
Let's see what happens if we just plot our data as is.
Indeed, we can barely see the difference between any category other than the top two.
11. The log scale
The logarithmic (or 'log') scale is a commonly used scale to plot data which solves just this problem.
It looks like this using the example of the number of internet hosts between 1981 and 2012.
What do you notice? You can see that each tick on the y-axis is no longer uniform and increasing evenly.
Instead, each tick is an order of magnitude bigger than the last.
12. Using log with our data
Let's use this nifty scale to solve our problem. Plotly has convenient arguments for easily transforming the scale to the log scale.
We will use the log_y argument, as our x-axis is categorical.
That looks much better. We can now see all our countries much clearer.
13. Log scale: a word of warning
Remember, when visualizing data, you are using it to tell a story.
If your audience may not know what a log scale is, they may misinterpret the values of each country to be much closer together than in reality,
so it is important to always keep your audience in mind when creating visualizations to share.
14. Let's practice!
Let's practice editing the axes of some basic plots in Plotly!