Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add caption & subtitle using plotly method in python

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()
like image 345
Maddy6 Avatar asked Sep 30 '19 10:09

Maddy6


People also ask

How do you add a caption in Powerpoint?

On the Playback tab, select Insert Captions, and then select Insert Captions. In the Insert Captions dialog box, select the file or files and then click Insert. If you need to add more caption files, just repeat the process.


2 Answers

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:

like image 80
InaMelloMood Avatar answered Sep 17 '22 14:09

InaMelloMood


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()
like image 34
vestland Avatar answered Sep 19 '22 14:09

vestland