我需要在向图形中添加文本的帮助。
我尝试了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', '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()
以下是输出:
我在两件事上需要帮助:
如果您还可以在第三张图表中说明如何修复被截断的y轴,那就太好了。现在,我必须更改标签以使其适应,这很耗时。有没有一种方法可以在容器中添加填充物或其他东西?
谢谢。
答案 0 :(得分:1)
这是一个不错的解决方法,但是在仔细检查可打印的python文档之后,我找不到任何可以完全满足您对所提供的可打印属性的要求的内容。如果您现在需要一次性快速修复,请尝试使用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,
),
您可以查看Plotly Annotations和Chart Attributes文档,以查看是否有更适合您需要的文档。
编辑:在将代码添加到问题之前,我开始发布此回复。这是一个示例,说明如何在所讨论的代码中为第一个图形的前两个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 的字符)来获得完美的对齐。您可以像this post中那样使用字符串的像素长度或其他度量来设计确定 x 的更准确方法,以使其正确对齐。希望有帮助。
答案 1 :(得分:1)
通过更改图形的边距,可以防止y轴在第三张图表中被截断。将以下代码添加到对go.Layout()
的调用中:
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
)
您可以为不同的y轴标签调整左页边距,也可以将其设置为根据最长标签的长度自动缩放。
答案 2 :(得分:1)
您可以将text
传递到go.Bar()
,在其中可以设置textposition="inside"
和insidetextanchor="start"
,这应该可以解决此问题。
fig = go.Figure(go.Bar(
x=[20, 14, 23],
y=['giraffes', 'orangutans', 'monkeys'],
orientation='h',
# define the annotations
text=['giraffes', 'orangutans', 'monkeys'],
# position, "auto", "inside" or "outside"
textposition="auto",
# anchor could be "start" or "end"
insidetextanchor="start",
insidetextfont=dict(family='Times', size=13, color='white'),
outsidetextfont=dict(family='Times', size=13, color='white')))
fig.update_layout(
yaxis=dict(
showticklabels=False,
))
fig.show()