Python Friday #195: Customise Your Plots in Plotly

There is still more to customise with Plotly than the colours or the templates. In this post we go through the few parts that Plotly does automatically but not always to our satisfaction.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

Name the axes

Plotly sets a name for the axes by default. When we want something else and not the column name of the DataFrame, we can change the names of our axes with the methods update_xaxes and update_yaxes:

This gives our plot the axes names we want:

Our bar plot now uses # Developers and Projects to label the axes.

 

Set a title

To set a title, we can use the title property and set it to the text we want:

This adds the title above of our plot:

Our plot now has the title Developers per project

 

Change the hover text

The speciality of Plotly is the interactivity of the plots. And one of the main points for that is the hoover text we can get on top of our points.

By default, the hoover text contains the values for x, y, and colour (if set) and uses the same background colour as the data point:

The hoover box contains the attributes (x, y, colour) of the data in the plot.

We can set a title for the hoover overlay and add a list of additional columns of our DataFrame we want to include:

Our hoover now shows the time as the title and includes the smoker and the size attribute:

The hoover shows the additional data in the overlay.

To change the colour of the hoover or the font, we can use the update_layout method on the figure and add a dictionary with our preferred values:

This gives us a hoover text with a larger font and a white background:

The hover is now white, uses a larger font and a different font type.

 

Change the bins or the range in your histogram

We can change the number of bins in our histogram with the nbins parameter:

This gives us a histogram with only 6 bins:

The histogram has only 6 instead of the 25 bins we get per default for the tips data set.

If we want to limit the range on the values on the x axis, we can use the parameter range_x:

This gives us a histogram with only the total_bill values between 20$ and 30$:

The histogram shows only the part between 20 and 30.

We can also limit the histogram for the count values with the range_y parameter:

This shows only columns in our histogram that are between 10 and 20:

The histogram is limited to the bars that are between 10 and 20 counts.

 

Next

With these customisations we can get our plots to match our requirements even better. Especially the changes on the hoover data may be of great use when you need to know more about a data point.

So far, we used a browser to interact with Plotly. Next week we look at a problem you may run into when you try to create and save plots in a code-only environment.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.