【问题标题】:Altair area plot with color determined by signAltair 区域图,颜色由符号确定
【发布时间】:2020-09-12 23:46:34
【问题描述】:

我想绘制一个图,在其中我对两条曲线之间的区域进行着色,颜色取决于哪条曲线较高(因此,例如,如果“曲线 1”高于“曲线”,则曲线之间的区域为绿色2”,如果“曲线 1”低于“曲线 2”,则为红色)。下面的情节是我制作这样一个情节的最大努力。结果接近我想要的,但是当我不想要这个时,它会连接具有相同符号的区域。 (最明显的例子是连接“x~0.6”和“x~1.1”的绿色区域。)有没有更好的方法来制作这样的情节?

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

df = pd.DataFrame({
    'x': np.linspace(0, 2, 100),
})
df['y1'] = np.sin(2 * np.pi * df.x)
df['y2'] = np.cos(2 * np.pi * df.x)
df['c'] = (df.y1 > df.y2).apply(lambda b: 'darkseagreen' if b else 'indianred')

base_chart = alt.Chart(df, width=1.618 * 400, height=400)
l1 = base_chart.mark_line(stroke='green', strokeWidth=4).encode(
    x='x:Q',
    y=alt.Y('y1:Q', axis=alt.Axis(title='y')),
)
l2 = base_chart.mark_line(stroke='red', strokeWidth=4).encode(
    x='x:Q',
    y='y2:Q',
)
a = base_chart.mark_area().encode(
    x='x:Q',
    y='y1:Q',
    y2='y2:Q',
    fill=alt.Color('c:N', scale=None),
)
alt.layer(a, l1, l2)

【问题讨论】:

    标签: altair


    【解决方案1】:

    我发现我可以使用impute 来防止绘制区域之间不需要的线条。

    import altair as alt
    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame({
        'x': np.linspace(0, 2, 100),
    })
    df['y1'] = np.sin(2 * np.pi * df.x)
    df['y2'] = np.cos(2 * np.pi * df.x)
    df['c'] = (df.y1 > df.y2).apply(lambda b: 'darkseagreen' if b else 'indianred')
    
    base_chart = alt.Chart(df, width=1.618 * 400, height=400)
    l1 = base_chart.mark_line(stroke='green', strokeWidth=4).encode(
        x='x:Q',
        y=alt.Y('y1:Q', axis=alt.Axis(title='y')),
    )
    l2 = base_chart.mark_line(stroke='red', strokeWidth=4).encode(
        x='x:Q',
        y='y2:Q',
    )
    a = base_chart.mark_area().encode(
        x='x:Q',
        y=alt.Y('y1:Q', impute={'value': None}),
        y2='y2:Q',
        fill=alt.Color('c:N', scale=None),
    )
    alt.layer(a, l1, l2)
    

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多