我希望自动确定子图条形上的标记线,即每个条形的完整宽度。目前,它们只是宽度的一部分:
这是代码:
import numpy as np
import plotly.graph_objs as go
from plotly.subplots import make_subplots
subplots = ['A','B','C','D']
fig = make_subplots(rows=2, cols=2, subplot_titles=subplots)
layout = go.Layout(
barmode='stack',
height=1000,
width=1000,
bargap=0.2,
)
fig.update_layout(layout)
directions = ['north','east','south','west']
levels = ['top','middle','bottom']
row = 1
col = 1
for subplot in subplots:
for direction in directions:
fig.add_trace(
go.Bar(
x=levels,
y=np.random.randint(20, size=3),
),
row=row,
col=col,
) ,
fig.add_trace(
go.Scatter(
mode='markers',
x=levels,
y=np.random.randint(20, size=3) + 2,
marker={
'symbol': 'line-ew-open',
'size': 24,
'color': 'black',
}
),
row=row,
col=col,
)
## move on
if col == 2:
row += 1
col = 1
else:
col += 1
fig.show()
根据r-beginnershere的回答,似乎可以使用
fig.full_figure_for_development()
以调整缩放的方式来做到这一点。但我不知道如何提取它。
任何帮助表示赞赏!
使用
fig.add_shape(type="line")
您可以在图形中插入可缩放的线条。通过对原始代码的更改,您将在所有子图中的每个条上得到线条。
import numpy as np
import plotly.graph_objs as go
from plotly.subplots import make_subplots
column_width = .6
subplots = ['A','B','C','D']
directions = ['north','east','south','west']
levels = ['top','middle','bottom']
fig = make_subplots(rows=2, cols=2, subplot_titles=subplots)
layout = go.Layout(
barmode='stack',
height=1000,
width=1000,
bargap=1-column_width,
)
fig.update_layout(layout)
row = 1
col = 1
for number, subplot in enumerate(subplots):
for direction in directions:
fig.add_trace(
go.Bar(
x=levels,
y=np.random.randint(20, size=3),
),
row=row,
col=col,
)
# add marker lines
bars_y = [bar.y for bar in fig.data[number*4:number*4+4]]
for pos,level in enumerate(levels):
y_pos = sum([a[pos] for a in bars_y])/4
fig.add_shape(type="line",
x0=pos-column_width/2,
x1=pos+column_width/2,
y0=y_pos,
y1=y_pos,
row=row,
col=col)
## move on
if col == 2:
row += 1
col = 1
else:
col += 1
fig.show(renderer="browser")
结果会是这样的。
这些是我已经检查过的替代方案。首先是
fig.add_hline
,但这会产生无限长度的线宽,这不是您想要的。 Sexond,添加标记对于长度来说是没有争议的。最后,深入研究 fig.full_figure_for_development()
后,您可以在布局的 xrange
部分找到 xaxis
。然而,这与您设置的范围相同。