Python 如何对齐plotly条形图上的左侧文本(包含示例图像)[plotly Dash]
我需要帮助在我的图表中添加文本 我试过text='y'和text position='inside',但是对于小条形图,文本是垂直的或是被压扁的,所以可以放在条形图中。我只是想把它写下来 下面是需要修复的代码的工作示例:Python 如何对齐plotly条形图上的左侧文本(包含示例图像)[plotly Dash],python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我需要帮助在我的图表中添加文本 我试过text='y'和text position='inside',但是对于小条形图,文本是垂直的或是被压扁的,所以可以放在条形图中。我只是想把它写下来 下面是需要修复的代码的工作示例: app = dash.Dash(__name__) app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'}) labels1 = ['0-7', '8-12'
app = dash.Dash(__name__)
app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'})
labels1 = ['0-7', '8-12', '13-15', '16-20', '21-25', '26+']
values1 = [10, 30, 10, 5, 6, 8]
labels2 = ['India', 'Scotland', 'Germany', 'NW England', 'N Ireland', 'Norway', 'NE England', 'Paris', 'North Africa', 'scandinavia']
values2 = [1, 0, 4, 9, 11, 18, 50, 7, 0, 2]
values3 = [10, 111, 75, 20]
labels4 = ['Safety Manager', 'Office Administrator', 'Internal Officer', 'Assistant Producer']
bar_color = ['#f6fbfc', '#eef7fa', '#e6f3f7', '#deeff5', '#d6ebf2', '#cde7f0', '#c5e3ed', '#bddfeb', '#b5dbe8', '#add8e6']
bar_color2 = ['#e6f3f7', '#deeff5', '#d6ebf2', '#cde7f0', '#c5e3ed', '#bddfeb', '#b5dbe8', '#add8e6']
app.layout = html.Div([
html.Div([
html.Div([
dcc.Graph(id = 'age',
figure = {
'data': [go.Bar(x = values1,
y = labels1,
orientation = 'h',
marker=dict(color = bar_color2),
text = labels1,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per tenure',
yaxis=dict(
zeroline=False,
showline=False,
showgrid = False,
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
)
)
}
)
], className = 'four columns'),
html.Div([
dcc.Graph(id = 'location',
figure = {
'data': [go.Bar(x = values2,
y = labels2,
orientation = 'h',
marker=dict(color = bar_color),
text = labels2,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per region',
yaxis=dict(
zeroline=False,
showline=False,
showgrid = False,
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
) )
}
)
], className = 'four columns'),
html.Div([
dcc.Graph(id = 'job',
figure = {
'data': [go.Bar(x = values3,
y = labels4,
orientation = 'h',
marker=dict(color = bar_color2),
text = labels4,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per role',
yaxis=dict(
# automargin=True,
zeroline=False,
showline=False,
showgrid = False,
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
)
)
}
)
], className = 'four columns')
], className = 'row')
])
if __name__ == '__main__':
app.run_server()
以下是输出:
以下是我希望文本外观的示例:
我需要两件事的帮助:
谢谢。这是一个不雅观的解决方法,但是在浏览了plotly python文档之后,我找不到任何东西能够完全满足您对plotly属性的要求。如果您现在需要一次性快速修复,请尝试使用
yaxis=dict(showticklabels=False)
手动添加标签作为注释,如:
layout = go.Layout(
# Hide the y tick labels
yaxis=dict(
showticklabels=False),
annotations=[
dict(
# I had to try different x values to get alignment
x=0.8,
y='giraffes',
xref='x',
yref='y',
text='Giraffes',
font=dict(
family='Arial',
size=24,
color='rgba(255, 255, 255)'
),
align='left',
# Don't show any arrow
showarrow=False,
),
我得到的输出如下所示:
您可以查看绘图和文档,看看是否有更适合您需要的内容
编辑:在将代码添加到问题之前,我就开始发布此回复。下面是一个示例,说明了如何对所讨论代码中第一个图形的前两个y标签进行注释:
app.layout = html.Div([
html.Div([
html.Div([
dcc.Graph(id = 'age',
figure = {
'data': [go.Bar(x = values1,
y = labels1,
orientation = 'h',
marker=dict(color = bar_color2),
text = labels1,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per tenure',
yaxis=dict(
zeroline=False,
showline=False,
showgrid = False,
showticklabels=False
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
)
),
annotations=[dict(
x=0.8,
y=labels1[0],
xref='x',
yref='y',
text=labels1[0],
font=dict(
family='Arial',
size=24,
color='rgba(255, 255, 255)'
),
align='left',
showarrow=False,
),
dict(
x=1.2,
y=labels1[1],
xref='x',
yref='y',
text=labels1[1],
font=dict(
family='Arial',
size=24,
color='rgba(255, 255, 255)'
),
align='left',
showarrow=False,
),
编辑2:@user8322222,要回答评论中的问题,您可以使用列表理解将注释字典如下所示:
annotations1 = [dict(x=(len(labels1[i])*0.15), y=labels1[i], xref='x', yref='y',
text=labels1[i], font=dict(family='Arial', size=24, color='rgba(255, 255, 255)'),
align='left', showarrow=False) for i in range(len(labels1))]
然而,我不认为会有一个常数,你可以乘以文本的字符长度(就像我在示例中用于x)来获得完美的对齐。您可以使用像素长度或字符串的其他度量,如中所示,设计一种更精确的确定x的方法,以使其正确对齐。希望这能有所帮助。您可以通过更改图形的边距来防止y轴在第三张图表中被截断。在调用
go.Layout()
的内部添加以下代码:
可以调整不同y轴标签的左边距,也可以将其设置为自动按最长标签的长度缩放
您可以将
text
传递到go.Bar()
,在那里您可以设置textposition=“inside”
和insideetextanchor=“start”
,这应该可以解决此问题
fig=go.Figure(go.Bar(
x=[20,14,23],
y=[‘长颈鹿’、‘猩猩’、‘猴子’],
方向='h',
#定义注释
text=[“长颈鹿”、“猩猩”、“猴子”],
#位置,“自动”、“内部”或“外部”
textposition=“自动”,
#锚点可以是“开始”或“结束”
insidetextanchor=“开始”,
insidetextfont=dict(family='Times',size=13,color='white'),
outsidetextfont=dict(family='Times',size=13,color='white'))
图1.2.2.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1(
yaxis=dict(
showticklabels=False,
))
图2(图3)
能否提供绘制有问题条形图的最小代码?@InonPeled Done。干杯。变量“html”是如何定义的?哇,这很有趣,谢谢。你知道有没有一种更快捷的方法可以通过标签循环来进行注释?谢谢,我使用了x=0和xref='paper'来垂直对齐所有文本。这些条形图连接到包含特定主题的下拉列表。如果我选择了一个没有印度的主题,例如,印度的注释仍然会出现在页面上,但图表没有。注释显示在其他条的间隙之间。你知道有什么办法吗?
margin=go.layout.Margin(
l=150, # left margin, in px
r=80, # right margin, in px
t=80, # top margin, in px
b=80, # bottom margin, in px
pad=0
)