我想用一些 KPI 卡制作董事会。每张卡片都是由面积图和指标组合而成的组合图。当我尝试向网格添加可视化时,出现错误:
Trace type 'indicator' is not compatible with subplot type 'xy' at grid position (1, 1)
我的代码:
fig = make_subplots(
rows=1
,cols=3
,specs=[[{"type": 'indicator', 'type' : 'xy'}, {"type": "indicator"}, {"type": "indicator"}]]
)
fig_add = fig.add_trace(
go.Indicator(
mode="number+delta"
,value=crime_stat.loc[crime_stat['year'] == 2016, 'assault_cnt'].max()
,number_font={
'size' : 36
,'color' : '#71797E'
}
,delta={
'reference' : crime_stat.loc[crime_stat['year'] == 2015, 'assault_cnt'].max()
,'relative' : True
,'valueformat' : '.1%'
,'font_size' : 24
})
,col=1
,row=1)
fig_add = fig.add_trace(
go.Scatter(
x=crime_stat['year']
,y=crime_stat['assault_cnt']
,fill='tozeroy'
,marker_color='#C6C5C5')
,col=1
,row=1)
fig_add.show()
我尝试指定
specs=[[{"type": 'indicator'}, {"type": "indicator"}, {"type": "indicator"}]]
。然后我得到一个错误:Trace type 'scatter' is not compatible with subplot type 'domain'
。
如果我尝试指定 specs=[[{"type": 'xy'}, {"type": "indicator"}, {"type": "indicator"}]]
,则会收到错误:Trace type 'indicator' is not compatible with subplot type 'xy'
。
我该如何解决这个问题?
我要添加的可视化示例:
我尝试猜测,由于指标位于某个区域,因此可能不需要指定迹线类型。图表已成功创建。我定制了参考示例并使用了从绘图中获取的示例数据。域值在 0 到 1 的范围内手动设置。
'x':[0.65,0.9]
的间距也会影响字母的大小。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
df = px.data.stocks()
#fig = go.Figure()
fig = make_subplots(rows=1, cols=2,
specs=[[{"type": 'scatter'}, {"type": "scatter"}]])
fig.add_trace(go.Scatter(
y = df.AAPL.tolist(), fill='tozeroy', line_color='red', opacity=0.3, showlegend=False), row=1, col=1)
fig.add_trace(go.Scatter(
y = df.GOOG.tolist(), fill='tozeroy',line_color='green', opacity=0.3, showlegend=False), row=1, col=2)
fig.add_trace(go.Indicator(
mode = "number+delta",
value = 492,
delta = {"reference": 512, "valueformat": ".0f"},
title = {"text": "APPL"},
domain = {'y': [0, 1], 'x': [0.1, 0.35]}))
fig.add_trace(go.Indicator(
mode = "number+delta",
value = 550,
delta = {"reference": 505, "valueformat": ".0f"},
title = {"text": "GOOG"},
domain = {'y': [0, 1], 'x': [0.65, 0.9]}))
fig.update_layout(xaxis={'range': [0, 62]}, template='plotly_white')
fig.update_layout(xaxis={'visible':False,'showgrid':False, 'showticklabels':False},
yaxis={'visible':False,'showgrid':False, 'showticklabels':False})
fig.update_layout(xaxis2={'visible':False,'showgrid':False, 'showticklabels':False},
yaxis2={'visible':False,'showgrid':False, 'showticklabels':False})
fig.show()