Altair: Arrows?

Created on 1 Jun 2018  Â·  7Comments  Â·  Source: altair-viz/altair

Is there any way to draw arrows? Imagine a simple vector of (Xo,Yo) --> (Xi, Yi) coordinates. How can I get the head of the arrow?

Thanks!

question vega-lite-related

Most helpful comment

It seems some support may have been added arrows/unicode characters was added in Vega-lite - see [here]?(https://vega.github.io/vega/examples/wind-vectors/)

Is this now possible in Altair?

I guess what I really want is to be able to annotate my plots with arrows - something like this, but much less complex. This is from an old this issue

56088534-ed5cb980-5e37-11e9-93d2-77e85f3a6002

All 7 comments

I do not see a builtin ability to do that.

Searching through the vega-lite issues it looks like the suggestion here is to use a text mark with a unicode character:
https://github.com/vega/vega/issues/974

Here is a small example:

import altair as alt
import pandas as pd

a = {'x': [2,3], 'y': [4,6], 'textof': ['➟', '➟']}
df = pd.DataFrame(a)

point = alt.Chart(df).mark_point().encode(
    x='x',
    y='y')

text = alt.Chart(df).mark_text(dx=-25,dy=19, angle=45, fontSize=35).encode(
    x='x',
    y='y',
    text='textof'
)

point + text

visualization 1

Thanks, but that is not what I want. Look the eigenvector plots in this link.
https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html

The only solution I have found is just to generate flat straight lines, and using and vector image processor I might manually add the heads in the lines.

Arrows are not yet supported in Vega-Lite, so they are not supported in Altair.

It seems some support may have been added arrows/unicode characters was added in Vega-lite - see [here]?(https://vega.github.io/vega/examples/wind-vectors/)

Is this now possible in Altair?

I guess what I really want is to be able to annotate my plots with arrows - something like this, but much less complex. This is from an old this issue

56088534-ed5cb980-5e37-11e9-93d2-77e85f3a6002

Hi, to 'plot' vectors I have been using mark_line + mark_point (triangle-shape) charts, but I think it is not very efficient. I was wondering if there is a better way to plot vectors with Altair, or if you are already working in this issue to make it possible.
This is an example of what I am doing:

# plot a vector in altair
# points
origin = [0,0]
vector1 = [3,-1]
vector2 = [2,3]

v = pd.DataFrame([origin, vector1, vector2], columns=['X','Y'])

# plots
lines = {}
for i in v.loc[v.index!=0].index:
    lines[i] = alt.Chart(v.loc[(v.index==0)|(v.index==i)]).mark_line().encode(
        alt.X('X', scale=alt.Scale(domain=(-4,4))), 
        alt.Y('Y', scale=alt.Scale(domain=(-4,4))))

heads = alt.Chart(v.loc[v.index!=0]).mark_point(
    shape='triangle', size=100, fill='royalblue').encode(x='X',y='Y')

lines[1] + lines[2] + heads

image

Is this now possible in Altair?

It looks like this was added to vega-lite in v4.9.0 while Altair currently uses v4.8.1 of vega-lite. I suspect that means that the next release of Altair will include support for this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Juan-132 picture Juan-132  Â·  3Comments

floringogianu picture floringogianu  Â·  3Comments

maxgerma picture maxgerma  Â·  3Comments

fischcheng picture fischcheng  Â·  4Comments

galloramiro picture galloramiro  Â·  3Comments