-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
721b2b2
commit 425e93a
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Arrow Vector | ||
------------------------------ | ||
This example shows a basic vector plot use point mark ``triangle`` as shape, | ||
other shape options can be found in :ref:`Point Mark<user-guide-point-marks>`. | ||
""" | ||
# category: case studies | ||
import altair as alt | ||
import numpy as np | ||
import pandas as pd | ||
|
||
vector1 = [0, 0, 3, -1] | ||
vector2 = [0, 0, 2, 3] | ||
|
||
v = pd.DataFrame([vector1, vector2], columns=['x1','y1','x2','y2']) | ||
|
||
# calculate the vector | ||
v['x_'] = v['x2'] - v["x1"] | ||
v['y_'] = v['y2'] - v["y1"] | ||
|
||
# calculate the angle between current vector and the default point mark direction (0,1) | ||
# dot product = (x_,y_) dot (0,1) = y_ | ||
v["norm"] = np.sqrt(v['x_']**2 + v['y_']**2) | ||
v["theta"] = np.degrees(np.arccos(v['y_']/v["norm"] )) | ||
|
||
|
||
lines = alt.Chart(v).mark_line().encode( | ||
x=alt.X("x1", scale=alt.Scale(domain=(-4, 4)), title='x'), | ||
y=alt.Y("y1", scale=alt.Scale(domain=(-4, 4)), title='y'), | ||
x2="x2", | ||
y2="y2" | ||
) | ||
|
||
wedge = alt.Chart(v).mark_point(shape="triangle", filled=True).encode( | ||
x="x2", | ||
y="y2", | ||
angle=alt.Angle("theta", scale=alt.Scale(domain=[0, 360])), | ||
size=alt.value(300), | ||
color=alt.value('#000000') | ||
) | ||
|
||
lines + wedge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Arrow Vector | ||
------------------------------ | ||
This example shows a basic vector plot use point mark ``triangle`` as shape, | ||
other shape options can be found in :ref:`Point Mark<user-guide-point-marks>` | ||
""" | ||
# category: case studies | ||
import altair as alt | ||
import numpy as np | ||
import pandas as pd | ||
|
||
vector1 = [0, 0, 3, -1] | ||
vector2 = [0, 0, 2, 3] | ||
|
||
v = pd.DataFrame([vector1, vector2], columns=['x1','y1','x2','y2']) | ||
|
||
# calculate the vector | ||
v['x_'] = v['x2'] - v["x1"] | ||
v['y_'] = v['y2'] - v["y1"] | ||
|
||
# calculate the angle between current vector and the default point mark direction (0,1) | ||
# dot product = (x_,y_) dot (0,1) = y_ | ||
v["norm"] = np.sqrt(v['x_']**2 + v['y_']**2) | ||
v["theta"] = np.degrees(np.arccos(v['y_']/v["norm"])) | ||
|
||
lines = alt.Chart(v).mark_line().encode( | ||
alt.X("x1") | ||
.title('x') | ||
.scale(domain=(-4, 4)), | ||
alt.Y("y1") | ||
.title('y') | ||
.scale(domain=(-4, 4)), | ||
alt.X2("x2"), | ||
alt.Y2("y2") | ||
) | ||
|
||
wedge = alt.Chart(v).mark_point(shape="triangle", filled=True).encode( | ||
alt.X("x2"), | ||
alt.Y("y2"), | ||
alt.Angle("theta") | ||
.scale(domain=[0, 360]), | ||
alt.SizeValue(300), | ||
alt.ColorValue('#000000') | ||
) | ||
|
||
lines + wedge |