How to set general font for graph with Plotly?

Question:

When plotting a graph with Plotly, is there a way to set a font for the whole figure at once (so that you don’t have to set it individually for each element in your graph)?

For MatPlotLib this is done with:

matplotlib.pyplot.rcparams["font.family"] = "Calibri"

For Plotly this is done with:

?????
Asked By: Matthi9000

||

Answers:

You should be able to do this with e.g.fig.layout.font.family = 'Arial'

This will be used as the default for all other fig.<anything>.font.family unless you explicitly override them.

Here is the documentation link: https://plot.ly/python/reference/#layout-font-family

If you want this set Plotly-wise you can use a Template: https://plot.ly/python/templates/

Answered By: nicolaskruchten

If you have a font (or any style really) that you want to apply to many figures

import plotly.figure_factory as ff

class Plot():
    def __init__(self):
        self.my_template = dict(
            layout=go.Layout(
                font=dict(family='Arial')
            )
        )


    def some_plot(self):
        #fig = ...
        fig.update_layout(template=self.my_template)
        return fig
Answered By: Kermit

That can be done editing templates:

import plotly.io as pio
import plotly.graph_objects as go

pio.templates["my_modification"] = go.layout.Template(
    layout=dict(font={"size": 20})
)

# Then combine your modification with any of the 
# available themes like this:
pio.templates.default = "plotly_white+my_modification"
Answered By: Soren
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.