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:
1 2 3 4 5 |
fig = px.bar(x=["a","b","c", "d"], y=[1,3,2,5]) fig.update_xaxes(title_text="Project") fig.update_yaxes(title_text="# Developers") fig.show() |
This gives our plot the axes names we want:
Set a title
To set a title, we can use the title property and set it to the text we want:
1 2 3 4 5 6 |
fig = px.bar(x=["a","b","c", "d"], y=[1,3,2,5], title="Developers per project") fig.update_xaxes(title_text="Project") fig.update_yaxes(title_text="# Developers") fig.show() |
This adds the title above of our plot:
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:
We can set a title for the hoover overlay and add a list of additional columns of our DataFrame we want to include:
1 2 3 4 5 6 7 |
fig = px.scatter(tips, x="total_bill", y="tip", color="day", hover_name="time", hover_data=["size", "smoker"]) fig.show() |
Our hoover now shows the time as the title and includes the smoker and the size attribute:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
fig = px.scatter(tips, x="total_bill", y="tip", color="day", hover_name="time", hover_data=["size", "smoker"]) fig.update_layout( hoverlabel=dict( bgcolor="white", font_size=16, font_family="Rockwell" ) ) fig.show() |
This gives us a hoover text with a larger font and a white background:
Change the bins or the range in your histogram
We can change the number of bins in our histogram with the nbins parameter:
1 2 3 4 |
fig = px.histogram(tips, x="total_bill", nbins=6) fig.show() |
This gives us a histogram with only 6 bins:
If we want to limit the range on the values on the x axis, we can use the parameter range_x:
1 2 3 4 5 |
fig = px.histogram(tips, x="total_bill", range_x=[20, 30] ) fig.show() |
This gives us a histogram with only the total_bill values between 20$ and 30$:
We can also limit the histogram for the count values with the range_y parameter:
1 2 3 4 5 |
fig = px.histogram(tips, x="total_bill", range_y=[10, 20] ) fig.show() |
This shows only columns in our histogram that are between 10 and 20:
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.