Python 如何将堆叠水平条形图中的跟踪文字与中心对齐

Python 如何将堆叠水平条形图中的跟踪文字与中心对齐,python,plotly,Python,Plotly,我有一个堆叠的水平条,我希望为每个轨迹定义的文本放置在相应条的中心。我找不到一个不使用anotations的属性来设置它,但是我想对每个跟踪使用“text”,并且能够对齐它 我将Plotly 3.4.1与Jupyter一起使用(Plotly离线)。 找不到任何关于如何执行此操作的文档,除了尝试使用注释执行此操作,如果我想精确定位显式坐标,这看起来是一个更合适的解决方案。我想要的是一个更简单的(类似于“align”:“center”),但在go.Bar下找不到任何属性 只希望“80”、“20”显示

我有一个堆叠的水平条,我希望为每个轨迹定义的文本放置在相应条的中心。我找不到一个不使用anotations的属性来设置它,但是我想对每个跟踪使用“text”,并且能够对齐它

我将Plotly 3.4.1与Jupyter一起使用(Plotly离线)。 找不到任何关于如何执行此操作的文档,除了尝试使用注释执行此操作,如果我想精确定位显式坐标,这看起来是一个更合适的解决方案。我想要的是一个更简单的(类似于“align”:“center”),但在go.Bar下找不到任何属性

只希望“80”、“20”显示在中心,而不是向右对齐

from plotly.offline import iplot, plot, init_notebook_mode
import plotly.graph_objs as go

def getStackedSentimentHbar():
    trace0 = go.Bar(
        y=["A","B"],
        x=[20,80],
        orientation = 'h',
        text=["20","80"],
        textposition="inside",
        hoverinfo = "none",
    )
    trace1 = go.Bar(
        y=["A","B"],
        x=[80,20],
        orientation = 'h',
        text=["80","20"],
        textposition="inside",
        hoverinfo = "none",
    )
    data = [trace0,trace1]
    layout = go.Layout(
        barmode='stack',
        showlegend=False,
        xaxis=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
            ticks='',
            showticklabels=False
        ),
        yaxis=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
            ticks='',
            showticklabels=True
        ),
        margin = dict(
            l = 200, 
            r = 50, 
            b = 50, 
            t = 50, 
            pad = 10
        ),
        font=dict(
            family='Heebo', 
            size=18, 
            color='#000000'
        )
    )
    fig = go.Figure(data=data, layout=layout)
    return fig

init_notebook_mode()
fig = getStackedSentimentHbar()
iplot(fig)

据我所知,Plotly中没有这样的参数,但我们总是可以破解Plotly:)

让我们只复制所有x和y值,但保留
文本的原样

在下面的代码中有两个函数,
GetStacked感性Bar
GetStacked感性BarCented
。第一个返回原始图形,第二个返回带有(几乎)居中标签的图形

from plotly.offline import iplot, plot, init_notebook_mode
import plotly.graph_objs as go

LAYOUT = go.Layout(
        barmode='stack',
        showlegend=False,
        xaxis=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
            ticks='',
            showticklabels=False
        ),
        yaxis=dict(
            showgrid=False,
            zeroline=False,
            showline=False,
            ticks='',
            showticklabels=True
        ),
        margin = dict(
            l = 200, 
            r = 50, 
            b = 50, 
            t = 50, 
            pad = 10
        ),
        font=dict(
            family='Heebo', 
            size=18, 
            color='#000000'
        )
    )

def getStackedSentimentHbar(values):

    data = []
    for i, x in enumerate(values['x']):
        trace = go.Bar(
            x=x,
            y=values['y'][i],
            orientation='h',
            text=x,
            textposition='inside',
            hoverinfo = 'none',
        )
        data.append(trace)

    fig = go.Figure(data=data, layout=LAYOUT)
    return fig

def getStackedSentimentHbarCentered(values):

    data = []
    for i, x in enumerate(values['x']):

        trace = go.Bar(
            x=[int(i / 2) for i in x * 2],
            y=values['y'][i] * 2,
            orientation = 'h',
            text=x,
            textposition='inside',
            hoverinfo = 'none'
        )
        data.append(trace)

    fig = go.Figure(data=data, layout=LAYOUT)
    return fig

values = {'x': [[20, 80], [80, 20]],
          'y': [['A', 'B'], ['A', 'B']]}

init_notebook_mode()
fig = getStackedSentimentHbarCentered(values)
iplot(fig)