简体   繁体   English

覆盖 altair 的折线图默认分组?

[英]override altair's default grouping for line chart?

I'd like to make a chart with one line with different colors at different spots along the line.我想用一条线在沿线的不同点制作不同颜色的图表。

This doesn't work:这不起作用:

import altair as alt
import numpy as np
import pandas as pd

x = np.linspace(0,1)
y = x**2
c = np.round(x*20)
df = pd.DataFrame({'x': x, 'y': y, 'c': c})
print(df.head())
alt.Chart(df).mark_line().encode(x='x', y='y', color='c')

图表

(I want one continuous line, not a bunch of segments.) (我想要一条连续的线,而不是一堆线段。)

Presumably the problem is that Altair (or Vega-Lite) decided to group by color and draw a separate line for each group.据推测,问题在于 Altair(或 Vega-Lite)决定按颜色分组并为每组画一条单独的线。

Can I override this default grouping to say that the entire line should be a single group?我可以覆盖这个默认分组说整行应该是一个组吗?

For comparison, in R's ggplot2 library the group argument would do what I'm looking for .为了进行比较,在 R 的 ggplot2 库中group参数可以满足我的要求

Here's a hack to get basically the plot I was looking for:这是一个黑客,基本上可以获得我正在寻找的情节:

import altair as alt
import numpy as np
import pandas as pd

x = np.linspace(0,1)
y = x**2
c = np.round(x*20)
df = pd.DataFrame({'x': x, 'y': y, 'c': c})
df_shifted = df.shift()
df_shifted['c'] = df['c']
alt.Chart(pd.concat([df, df_shifted])).mark_line().encode(x='x', y='y', color='c')

This approach gives up on telling Altair/Vega-Lite that I want my points to make up a single line, and instead makes one separate segment between every pair of points by using an extra (shifted) copy of the data.这种方法放弃告诉 Altair/Vega-Lite 我希望我的点组成一条线,而是通过使用数据的额外(移位)副本在每对点之间创建一个单独的段。

The line segment is colored based on the left point in each segment, which I guess is a little misleading (very misleading if there were only a few points).线段根据每个线段中的左点进行着色,我想这有点误导(如果只有几个点,则非常误导)。 You could make the hack more complicated by making the color the average of the two sides.您可以通过使颜色为两侧的平均值来使 hack 更加复杂。

在此处输入图片说明

Still hoping someone will answer definitively on whether the user can control the grouping.仍然希望有人能明确回答用户是否可以控制分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM