Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altair - draw a line in plot where x = y

This snippet:

chart = alt.Chart(df).mark_point(filled=True).encode(
      alt.X('Goals Conceded:Q'),
      alt.Y('Goals:Q'),
      alt.Size('Goals:Q', legend=None, scale=alt.Scale(range=[0, 1500])),
      alt.Color('Color', legend=None, scale=None),
      tooltip = [alt.Tooltip('For Team:N'),
                alt.Tooltip('Goals:Q'),
                alt.Tooltip('Goals Conceded:Q')]
      ).properties(
          width=800,
          height=600
      )

Plots:

enter image description here

Now I would like to manually add a line, where x = y, in order to get the following result:

enter image description here


How can I do this?

like image 1000
8-Bit Borges Avatar asked Aug 31 '25 10:08

8-Bit Borges


1 Answers

You could add a dummy line:

line = pd.DataFrame({
    'Goals Conceded': [0, 2],
    'Goals': [0, 2],
})

line_plot = alt.Chart(line).mark_line(color= 'red').encode(
    x= 'Goals Conceded',
    y= 'Goals'.
)

chart + line_plot

I don't have your dataset, so below is an example mostly borrowed from Altair Example Gallery:

import pandas as pd
import altair as alt
from vega_datasets import data

source = data.iris()

iris_plot = alt.Chart(source).mark_circle().encode(
    alt.X('sepalLength'),
    alt.Y('sepalWidth'),
    color='species',
    size='petalWidth'
)

line = pd.DataFrame({
    'sepalLength': [0, 5],
    'sepalWidth':  [0, 5],
})

line_plot = alt.Chart(line).mark_line(color= 'red').encode(
    x= 'sepalLength',
    y= 'sepalWidth',
)

iris_plot + line_plot

enter image description here

like image 88
Kazuhito Avatar answered Sep 03 '25 00:09

Kazuhito