Plotly Set Bold font

Text is an incredible feature when working with plots. They allow you to provide descriptive information to accompany the figures, such as titles, labels, and etc.

Therefore, when working with text in your plots, you may need some customization, such as adjusting the font size, font family, and more.

In this tutorial, we will show you how you can customize the font of various elements, including formatting features such as Bold, Italics, and etc.

Plotly Set Bold Font

Let us start by creating a simple example to illustrate. For this one, we will use a simple scatter plot using Plotly Express.

The sample code is as provided below:

import as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.update_layout(title="Sample Scatter Plot")

In this example, we create a simple scatter plot using random values. The code above should return a figure as shown below:

Suppose we want to set the title in bold font? For that, we can use the HTML tags to accomplish this. However, keep in mind that this might depend heavily on the rendering environment.

In our example, we are using Plotly.js which allows us to render HTML. Therefore, we can use the update_layout() function and pass the title inside a bold tag.

The example code is as shown below:

fig.update_layout(title="<b>Sample Scatter Plot</b>")

Placing the text inside an opening and closing bold tags should result in the figure as shown below:

Note that the title text is set to bold.

For italics, you can accomplish the same by setting the tags to italicized as:

fig.update_layout(title="<i>Sample Scatter Plot</i>")

To set the text as bold and italicized, use the <em> tag as shown:

fig.update_layout(title="<em>Sample Scatter Plot</em>")

This should return a figure as shown below:

Plotly Set Bold Font – Graph_Objects

We can also set a text to bold when using Plotly’s graph_objects. We do this using the Layout() function as shown in the example code below:

import plotly.graph_objects as go
data = [go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16], mode='markers')]
layout = go.Layout(title="<b>Sample Scatter Plot</b>")
fig = go.Figure(data=data, layout=layout)

In graph_objects, we define the type of plot we wish to create using the data variable. This allows us to specify all the parameters we wish to include.

Then, we set the customization for the plot in the layout variable using the graph_objects.Layout() function.

We set the plot title as bold text in our example using HTML tags. The resulting figure is shown below:

With that, you have successfully discovered how to set a bold font using Plotly express and graph_objects.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list