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() 

3 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:

2

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() 

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

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like