【问题标题】:Altair add log linear regression to chart with selectionAltair 通过选择将对数线性回归添加到图表
【发布时间】:2021-09-03 08:58:59
【问题描述】:

我有一个 Altair 散点图,绘制日期与数量。 该图表包括一个选择菜单,用于突出显示来自不同领域的点。

我想在上面添加一个逻辑回归,transform_regression

但是我在执行此操作时遇到错误。

这是我的代码:

import altair as alt
alt.themes.enable('fivethirtyeight')
selection = alt.selection_multi(fields=['Domain'], bind='legend')
chart = alt.Chart(df, width=1100, height=600, 
          title="Parameter count of ML systems through time")\
.mark_point(size=120, filled=False).encode(
  x=alt.X('Publication date:T'),
  y=alt.Y('Parameters:Q',scale=alt.Scale(type='log'), axis=alt.Axis(format=".1e")),
  color='Domain',
  shape = 'Domain',
  tooltip=['System', 
           'Reference', 
           'Publication date', 
           alt.Tooltip('Parameters', format=".1e"), 
           'Domain'],
  opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_selection(
    selection
)

regression = chart.transform_regression(
    on="Publication date", regression="Parameters", groupby=["Domain"]
).mark_line()

alt.layer(chart, regression).configure_axis(labelFontSize=20,titleFontSize=30).configure_legend(
    titleFontSize=20,
    labelFontSize =18,
    gradientLength=400,
    gradientThickness=30,
    symbolSize = 130
)

这是我得到的错误:Javascript Error: Duplicate signal name: "selector027_tuple" This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

我无法理解 javascript 控制台的输出,但它显示了这一点:

Uncaught (in promise) Javascript Error: Duplicate signal name: "selector028_tuple"<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.
Promise.catch (async)
displayChart @ VM1182:32
(anonymous) @ VM1182:45
load (async)
(anonymous) @ VM1182:19
loadScript @ VM1182:15
(anonymous) @ VM1182:43

【问题讨论】:

    标签: python altair


    【解决方案1】:

    由于回归图基于已添加选择的点图,因此在将这两个图表分层时添加了两次。相反,创建没有选择的点图表并将其仅添加到图层中的一个图表中:

    alt.layer(chart.add_selection(selection), regression)
    

    或一次到分层图表:

    alt.layer(chart, regression).add_selection(selection)
    

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多