如何在python中使用plotly方法添加标题和字幕

问题描述 投票:1回答:2

我正在尝试使用plotly绘制条形图,我想添加标题和字幕。(您可以在此处选择任意示例来添加标题和字幕)我绘制条形图的代码:

import plotly.graph_objects as go  

fig = go.Figure()       

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

fig.show()
python plotly
2个回答
0
投票

使用fig.update_layout(title_text='Your title')作为字幕。字幕没有内置选项。但是您可以通过将x轴标签移到顶部并同时在右下角插入注释来获得所需的效果。我也尝试过使用其他y值,但是似乎没有办法将注释移出图本身。您还可以更改标题和字幕的字体,以使其在其余标签中脱颖而出。

简介:

enter image description here

产品编号:

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

0
投票

也许是这样?

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

© www.soinside.com 2019 - 2024. All rights reserved.