我有一个堆叠的水平条,我希望为每个迹线定义的文本都放置在相应条的中心。我找不到不使用注释即可设置此属性的属性,但我想为每个跟踪使用“文本”并能够对齐它。
我在Jupyter上使用Plotly 3.4.1(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)
答案 0 :(得分:1)
据我所知,Plotly中没有这样的参数,但是我们总是可以破解Plotly的:)
让我们只复制所有x和y值,但保留text
不变。
在下面的代码中,有两个功能,getStackedSentimentHbar
和getStackedSentimentHbarCentered
。第一个返回原始图形,第二个返回带有(几乎)居中标签的图形。
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)
答案 1 :(得分:0)
更好的方法 - 使用单独的注释。
var x1A = 20;
var x1B = 80;
var x2A = 80;
var x2B = 20;
var trace1 = {
y: ['A', 'B'],
x: [x1A, x1B],
type: 'bar',
orientation: 'h',
};
var trace2 = {
y: ['A', 'B'],
x: [x2A, x2B],
type: 'bar',
orientation: 'h',
};
var data = [trace1, trace2];
var layout = {
barmode: 'stack',
annotations: [
{ x: x1A / 2, y: 'A', text: '20', showarrow: false },
{ x: x1A + x2A / 2, y: 'A', text: '80', showarrow: false },
{ x: x1B / 2, y: 'B', text: '80', showarrow: false },
{ x: x1B + x2B / 2, y: 'B', text: '20', showarrow: false },
],
};
Plotly.newPlot('myDiv', data, layout);
<head>
<!-- Load plotly.js into the DOM -->
<script src='https://cdn.plot.ly/plotly-2.0.0.min.js'></script>
</head>
<body>
<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
以下是 Plotly 官方文档中的示例:https://plotly.com/python/horizontal-bar-charts/#color-palette-for-bar-chart