在下面添加标题 Python Plotly Choropleth Map

Adding caption below Python Plotly Choropleth Map

我想在我的 plotly choropleth 地图下面添加一个标题(使用 Python)。我研究过使用带有 graph_objs 的注释,但它似乎只适用于地图区域内的位置。有没有办法让注释显示在分区统计图下方 and/or 有没有其他方法可以做到这一点?

现在我得到了这个,但希望标题显示在地图区域下方:

我试过输入小于 0 的 y 值,但文本注释根本不显示。

这是我的代码:

import plotly.graph_objects as go
import pandas as pd
import plotly

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

fig = go.Figure(data=go.Choropleth(
    locations = df['CODE'],
    z = df['GDP (BILLIONS)'],
    text = df['COUNTRY'],
    colorscale = 'Blues',
    autocolorscale=False,
    reversescale=True,
    marker_line_color='darkgray',
    marker_line_width=0.5,
    colorbar_tickprefix = '$',
    colorbar_title = 'GDP<br>Billions US$',
))

fig.update_layout(
    title_text='2014 Global GDP',
    geo=dict(
        showframe=False,
        showcoastlines=False,
        projection_type='equirectangular'
    ),
    annotations = [dict(
        x=0.5,
        y=0,    #Trying a negative number makes the caption disappear - I'd like the caption to be below the map
        xref='paper',
        yref='paper',
        text='Source: <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
            CIA World Factbook</a>',
        showarrow = False
    )]
)

fig.show()

设置 y=-0.1 在我这边工作正常:

情节 1

如果出于某种原因您的情况并非如此(也许是版本问题?),您应该尝试将其保留在 y=0 而不是通过调整边距在图形下方腾出空间情节是这样的:

fig.update_layout(
    margin=dict(l=20, r=20, t=60, b=20),
    paper_bgcolor="LightSteelBlue")

情节 2:

完整代码:

import plotly.graph_objects as go
import pandas as pd
import plotly

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

fig = go.Figure(data=go.Choropleth(
    locations = df['CODE'],
    z = df['GDP (BILLIONS)'],
    text = df['COUNTRY'],
    colorscale = 'Blues',
    autocolorscale=False,
    reversescale=True,
    marker_line_color='darkgray',
    marker_line_width=0.5,
    colorbar_tickprefix = '$',
    colorbar_title = 'GDP<br>Billions US$',
))

fig.update_layout(
    title_text='2014 Global GDP',
    geo=dict(
        showframe=False,
        showcoastlines=False,
        projection_type='equirectangular'
    ),
    annotations = [dict(
        x=0.5,
        y=0,    #Trying a negative number makes the caption disappear - I'd like the caption to be below the map
        xref='paper',
        yref='paper',
        text='Source: <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
            CIA World Factbook</a>',
        showarrow = False
    )]
)

fig.update_layout(
    margin=dict(l=20, r=20, t=60, b=20),
    paper_bgcolor="LightSteelBlue")

fig.show()