22

I'm trying to plot a bar chart using plotly and I wanted to add a caption and subtitle.(Here you can take any example of your choice to add caption and subtitle)
My code for plotting the bar chart:

import plotly.graph_objects as go  

fig = go.Figure()       

fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500])) 

fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305
Maddy6
  • 367
  • 1
  • 3
  • 14

3 Answers3

57

Plotly takes your string and passes it as HTML. Adding HTML in the title string or X axis string lets you put in some quick subtitles/captions in both ploty graph objects and plotly express.

<br> is a line break, and <sup> is superscript, which lets you quickly make a smaller subtitle or caption.

graph objects:

import plotly.graph_objects as go  

fig = go.Figure()       

fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500]))
fig.update_layout(
    title=go.layout.Title(
        text="Plot Title <br><sup>Plot Subtitle</sup>",
        xref="paper",
        x=0
    ),
        xaxis=go.layout.XAxis(
        title=go.layout.xaxis.Title(
            text="Fruits<br><sup>Fruit sales in the month of January</sup>"
            )
        )
    )

fig.show()

plotly express:

import plotly.express as px
fig = px.bar(
    x=["Apple", 'Mango', 'Banana'], 
    y=[400, 300, 500],
    title = "Plot Title <br><sup>Plot Subtitle</sup>",
    labels = {'x':"Fruits<br><sup>Fruit sales in the month of January</sup>", 
              'y':'count'}
)
fig.show()

figure:

Mig B
  • 637
  • 1
  • 11
  • 19
InaMelloMood
  • 671
  • 4
  • 3
7

Use fig.update_layout(title_text='Your title') for your caption. There's no built-in option for subtitles. But you can get the desired effect by moving the x-axis labels to the top and at the same time insert an annotation at the bottom right. I've tried with other y-values as well, but there doesn't seem to be a way to get the annotations outside the plot itself. You could also change the fonts of the caption and subtitle to make them stand out from the rest of the labels.

Plot:

enter image description here

Code:

import plotly.graph_objects as go  

fig = go.Figure()       

fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500])) 


fig.update_layout(title=go.layout.Title(text="Caption", font=dict(
                family="Courier New, monospace",
                size=22,
                color="#0000FF"
            )))

fig.update_layout(annotations=[
       go.layout.Annotation(
            showarrow=False,
            text='Subtitle',
            xanchor='right',
            x=1,
            xshift=275,
            yanchor='top',
            y=0.05,
            font=dict(
                family="Courier New, monospace",
                size=22,
                color="#0000FF"
            )
        )])

fig['layout']['xaxis'].update(side='top')

fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305
2

Maybe something like this?

import plotly.graph_objects as go  

fig = go.Figure()       

fig.add_trace(go.Bar(x=["Apple", 'Mango', 'Banana'], y=[400, 300, 500])) 
fig.update_layout(
    title=go.layout.Title(
        text="Plot Title",
        xref="paper",
        x=0
    ),
    xaxis=go.layout.XAxis(
        title=go.layout.xaxis.Title(
            text="x Axis",
            font=dict(
                family="Courier New, monospace",
                size=18,
                color="#7f7f7f"
            )
        )
    ),
    yaxis=go.layout.YAxis(
        title=go.layout.yaxis.Title(
            text="y Axis",
            font=dict(
                family="Courier New, monospace",
                size=18,
                color="#7f7f7f"
            )
        )
    )
)
fig.show()

Example

ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14
  • 1
    I'm interested in adding the caption at the bottom of the chart.(Like we do to add Source of the graph) – Maddy6 Sep 30 '19 at 11:44