Open In App

Setting Bold Font Style in Plotly

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Plotly is a versatile and powerful graphing library for Python that allows users to create interactive and visually appealing plots. One of the key aspects of creating effective visualizations is the ability to customize text and fonts, including setting bold font styles. This article will guide you through various methods to set bold font styles in Plotly, covering titles, axis labels, annotations, and more.

Setting Bold Font Style for Titles in Plotly

1. Using HTML Tags

One of the simplest ways to set bold font styles in Plotly is by using HTML tags. This method is particularly useful for setting bold text in titles and annotations.

Example: Setting Bold Title Using HTML Tags

Python
import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2])])
fig.update_layout(title='<b>Graphs -Bold Title</b>')
fig.show()

Output:

html
Using HTML Tags

In this example, the <b> HTML tag is used to make the title bold.

2. Using Font Family

Another method to achieve bold text is by specifying a bold font family. Common bold font families include "Arial Black" and "Helvetica Bold".

Example: Setting Bold Font Family

Python
import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2])])
fig.update_layout(title='Bold Title - Graphs', title_font=dict(family='Arial Black', size=24))
fig.show()

Output:

html-(1)
Using Font Family

In this example, the title font is set to "Arial Black" to make it bold.

3. Customizing Axis Labels

You can also set bold font styles for axis labels using similar methods. This can be done by updating the tickfont property of the axes.

Example: Setting Bold Axis Labels

Python
import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2])])
fig.update_layout(
    title='Graphs',
    xaxis=dict(tickfont=dict(family='Arial Black', size=14)),
    yaxis=dict(tickfont=dict(family='Arial Black', size=14))
)
fig.show()

Output:

html-(2)
Customizing Axis Labels

In this example, both the x-axis and y-axis labels are set to use the "Arial Black" font family.

4. Bold Annotations

Annotations are text elements that can be placed anywhere on the plot. You can set the font style of annotations to bold using the font property.

Example: Setting Bold Annotations

Python
import plotly.graph_objects as go

fig = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[4, 5, 6])])
fig.add_annotation(
    x=2, y=5,
    text="Bold Annotation",
    font=dict(family='Arial Black', size=16, color='black')
)
fig.show()

Output:

html-(3)
Setting Bold Annotations

In this example, an annotation with bold text is added to the plot.

5. Global Font Settings

You can set a global font style that applies to all text elements in the plot. This can be done using the layout.font property.

Example: Setting Global Bold Font

Python
import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2])])
fig.update_layout(font=dict(family='Arial Black', size=14))
fig.show()

Output:

html-(4)
Global Font Settings

In this example, all text elements in the plot will use the "Arial Black" font family.

Advanced Customization of Text Elements in Plotly

1. Using texttemplate for Custom Text

The texttemplate property allows for more advanced customization of text elements in Plotly. This can be particularly useful for setting bold text in specific parts of the plot.

Example: Customizing Text with texttemplate

Python
import plotly.graph_objects as go

fig = go.Figure(go.Pie(
    values=[40000000, 20000000, 30000000, 10000000],
    labels=["Wages", "Operating expenses", "Cost of sales", "Insurance"],
    texttemplate="<b>%{label}</b>: %{value:$,s} <br>(%{percent})",
    textposition="inside"
))
fig.show()

Output:

plotly-(1)
Customizing Text with texttemplate

In this example, the texttemplate property is used to set bold text for the labels inside a pie chart.

2. Combining Multiple Font Styles

You can combine multiple font styles, such as bold and italic, by using HTML tags or by specifying different font properties.

Example: Combining Bold and Italic Styles

Python
import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2])])
fig.update_layout(
    title='<b><i>Bold and Italic Title</i></b>',
    title_font=dict(family='Arial Black', size=24)
)
fig.show()

Output:

html-(5)
Combining Bold and Italic Styles

In this example, both bold and italic styles are applied to the title using HTML tags.

Conclusion

Setting bold font styles in Plotly is a straightforward process that can significantly enhance the readability and visual appeal of your plots. Whether you are customizing titles, axis labels, annotations, or using global font settings, Plotly provides flexible options to achieve the desired text styling. By leveraging these methods, you can create professional and visually engaging plots that effectively communicate your data insights.


Next Article

Similar Reads