- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个时间序列的成本值,我想获得选定日期范围内的成本总和。间隔选择工作正常,但我如何访问所选日期?
brush = alt.selection(type='interval', encodings=['x'])
chart = alt.Chart(forecast).mark_bar().encode(
x='dates:T',
y='costs:Q',
color='type:N',
).add_selection(
brush
)
最佳答案
您可以将文本标记与 filter transform 一起使用以显示选择范围内的值的总和。例如:
import altair as alt
import pandas as pd
import numpy as np
data = pd.DataFrame({
"dates": pd.date_range('2019-04-01', freq='M', periods=20),
"costs": np.linspace(1000, 5000, 20),
})
brush = alt.selection(type='interval', encodings=['x'])
chart = alt.Chart(data).mark_bar().encode(
x='dates:T',
y='costs:Q',
).add_selection(
brush
)
text = alt.Chart(data).transform_filter(brush).mark_text(
align='left',
baseline='top',
).encode(
x=alt.value(5),
y=alt.value(5),
text=alt.Text('sum(costs):Q', format='.1f'),
)
chart + text
编辑:如果你想在文本中显示选择范围,这并不完全简单,但你可以使用 calculate transform 来实现。连同适当的 vega expression按名称引用选择的字符串。
例如:
brush = alt.selection(type='interval', encodings=['x'], name='sel')
chart = alt.Chart(data).mark_bar().encode(
x='dates:T',
y='costs:Q',
).add_selection(
brush
)
text = alt.Chart(data).transform_filter(
brush
).transform_aggregate(
total='sum(costs)'
).transform_calculate(
date_range="sel.dates ? monthAbbrevFormat(month(sel.dates[0])) + ' to ' + monthAbbrevFormat(month(sel.dates[1])) : 'all'",
text="'Total for ' + datum.date_range + ': ' + format(datum.total, '.0f')"
).mark_text(
align='left',
baseline='top',
).encode(
x=alt.value(5),
y=alt.value(5),
text=alt.Text('text:N'),
)
chart + text
关于python - 在 Altair 中获取选择总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60893866/
给定一个交互式面积图,如下所示: import altair as alt from vega_datasets import data source = data.iowa_electricity(
有一个散点图,我知道您在比例尺上使用绑定(bind)进行平移并使用滚轮进行缩放,这很棒。但是,一旦缩放,就需要一种方法来进行选择间隔,而无需进一步缩放效果。例如,需要一种通过 shift 键暂停或退出
[![图表显示没有正确格式的数字][1]][1] 我需要格式化这些条上的标签,以便将它们四舍五入到最接近的整数。我有以下代码: def chart_tender_response_times(data
这是对 Altair: Sorting faceted “text” chart not reflecting expectation 的跟进查询.我用一列额外的列修改了数据框。我的目标是对“标记”列
我正在尝试按照 Multi-Line Tooltip example 的方式创建一个图表,但我想格式化正在打印的字符串以在末尾添加一些文本。我正在尝试修改这部分: # Draw text labels
我正在尝试在 VL/Altair 中制作如下图表: 在本例中,X 轴标签的功能类似于 VL headers据我所知。在 VL/Altair 中,标题会自动设置在每个子图的顶部(例如, this cha
我有一个数据框,我想根据下拉菜单对其进行子选择。所以给定以下代码: import pandas as pd import altair as alt from vega_datasets import
这是取自 Altair documentation 的代码关于配色方案。 alt.Chart(iris).mark_point().encode( x='petalWidth', y=
如果可能,我想使用 altair 创建一个水平条形图,其中包含水平连接并与条形对齐的表格中的一列或多列。我正在粘贴一个快速 Excel 图表的示例,以大致了解我想要什么。 以下示例来自您的网站(代码和
我有一个包含一堆分箱数据的直方图,我想知道如果我从直方图中选择一个条形图是否可以生成一个表,它会显示原始数据框中的数据。 最佳答案 您可以使用 mark_text 创建表格的外观。这是 an exam
我想绘制具有多个 y 轴的平行坐标图。我已经在 Vega-Lite 中找到了如何做到这一点 here但我还没有找到使用 Altair 的方法,只有一个 very simple example其中所有
对于下面的示例,我想使用绿色配色方案进行导出,使用红色进行导入。当我单独创建图表时,一切都很好,它们得到了我分配给它们的配色方案。然而,当我连接图表时,他们都得到了红色方案。 import panda
当使用日期字段对颜色进行编码时,色标范围不会跨越整个配色方案渐变范围。例如,这个: alt.Chart(mydf).mark_point().encode( x='date:T', y
有没有办法让 Python Altair 绘图显示两个偏差标准?使用 mark_errorbar(extent='stdev') 仅显示一个标准偏差。 # Only shows one standar
我正在尝试在 Altair 上绘制以下内容:x = 每日时间戳y = 记录数,因为数据是名义上的而非定量的 当我尝试按月或季度汇总 x 时,是否可以将 y 显示为每天的平均记录数而不是总和? char
这个 question解决如何使用 mark_text() 访问和显示 R2 值 我对访问和显示系数感兴趣。将 rSquared 替换为 coef 会产生截距和斜率的扁平数组,如 documentat
无法为以下直方图中的分箱值着色。我打算对 x 轴上小于 50 的所有条进行着色(信用度)。 Altair 是如何做到的? base = alt.Chart(X_train) histogram = b
当我绘制由两组线组成的分层图表时,一层中的工具提示不显示。这也发生在 VL 编辑器中。任何关于为什么会发生这种情况的见解将不胜感激。 这是一个可重现的例子来演示这个问题(实际上我在第一层有更多行):
我可以修改经典Simple Scatter Plot with Tooltips , 为标记添加不透明度,但我想图例颜色保持 100% 不透明。在我尝试制作的图表中,我有一个包含数万行的 df。 im
我希望使用 altair 创建一个类似于附图的图,该图是使用 matplotlib 绘制的。基本上,一条绘制平均值的线和一个置信区间。 我的数据框看起来像这样。 dfr = pd.DataFrame(
我是一名优秀的程序员,十分优秀!