-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for line-based annotations (#151)
* feat: add support line-based annotations * docs: describe `scatter.annotations()`
- Loading branch information
Showing
18 changed files
with
656 additions
and
6 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
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
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,96 @@ | ||
# Annotations | ||
|
||
To help navigating and relating points and clusters, Jupyter Scatter offers | ||
annotations such a lines and rectangles. | ||
|
||
::: info | ||
Currently, Jupyter Scatter supports line-based annotations only. In the future, | ||
we plan to add support for text annotations as well. | ||
::: | ||
|
||
## Line, HLine, VLine, and Rect | ||
|
||
To draw annotations, create instances of `Line`, `HLine`, `VLine`, or `Rect`. | ||
You can then either pass the annotations into the constructor, as shown below, | ||
or call `scatter.annotations()`. | ||
|
||
```py{9-17,22} | ||
import jscatter | ||
import numpy as np | ||
x1, y1 = np.random.normal(-1, 0.2, 1000), np.random.normal(+1, 0.05, 1000) | ||
x2, y2 = np.random.normal(+1, 0.2, 1000), np.random.normal(+1, 0.05, 1000) | ||
x3, y3 = np.random.normal(+1, 0.2, 1000), np.random.normal(-1, 0.05, 1000) | ||
x4, y4 = np.random.normal(-1, 0.2, 1000), np.random.normal(-1, 0.05, 1000) | ||
y0 = jscatter.HLine(0) | ||
x0 = jscatter.VLine(0) | ||
c1 = jscatter.Rect(x_start=-1.5, x_end=-0.5, y_start=+0.75, y_end=+1.25) | ||
c2 = jscatter.Rect(x_start=+0.5, x_end=+1.5, y_start=+0.75, y_end=+1.25) | ||
c3 = jscatter.Rect(x_start=+0.5, x_end=+1.5, y_start=-1.25, y_end=-0.75) | ||
c4 = jscatter.Rect(x_start=-1.5, x_end=-0.5, y_start=-1.25, y_end=-0.75) | ||
l = jscatter.Line([ | ||
(-2, -2), (-1.75, -1), (-1.25, -0.5), (1.25, 0.5), (1.75, 1), (2, 2) | ||
]) | ||
scatter = jscatter.Scatter( | ||
x=np.concatenate((x1, x2, x3, x4)), x_scale=(-2, 2), | ||
y=np.concatenate((y1, y2, y3, y4)), y_scale=(-2, 2), | ||
annotations=[x0, y0, c1, c2, c3, c4, l], | ||
width=400, | ||
height=400, | ||
) | ||
scatter.show() | ||
``` | ||
|
||
<div class="img simple"><div /></div> | ||
|
||
## Line Color & Width | ||
|
||
You can customize the line color and width of `Line`, `HLine`, `VLine`, or | ||
`Rect` via the `line_color` and `line_width` attributes. | ||
|
||
```py | ||
y0 = jscatter.HLine(0, line_color=(0, 0, 0, 0.1)) | ||
x0 = jscatter.VLine(0, line_color=(0, 0, 0, 0.1)) | ||
c1 = jscatter.Rect(x_start=-1.5, x_end=-0.5, y_start=+0.75, y_end=+1.25, line_color="#56B4E9", line_width=2) | ||
c2 = jscatter.Rect(x_start=+0.5, x_end=+1.5, y_start=+0.75, y_end=+1.25, line_color="#56B4E9", line_width=2) | ||
c3 = jscatter.Rect(x_start=+0.5, x_end=+1.5, y_start=-1.25, y_end=-0.75, line_color="#56B4E9", line_width=2) | ||
c4 = jscatter.Rect(x_start=-1.5, x_end=-0.5, y_start=-1.25, y_end=-0.75, line_color="#56B4E9", line_width=2) | ||
l = jscatter.Line( | ||
[(-2, -2), (-1.75, -1), (-1.25, -0.5), (1.25, 0.5), (1.75, 1), (2, 2)], | ||
line_color="red", | ||
line_width=3 | ||
) | ||
``` | ||
|
||
<div class="img styles"><div /></div> | ||
|
||
<style scoped> | ||
.img { | ||
max-width: 100%; | ||
background-position: center; | ||
background-repeat: no-repeat; | ||
background-size: cover; | ||
} | ||
|
||
.img.simple { | ||
width: 316px; | ||
background-image: url(/images/annotations-simple-light.png) | ||
} | ||
.img.simple div { padding-top: 83.2278481% } | ||
|
||
:root.dark .img.simple { | ||
background-image: url(/images/annotations-simple-dark.png) | ||
} | ||
|
||
.img.styles { | ||
width: 314px; | ||
background-image: url(/images/annotations-styles-light.png) | ||
} | ||
.img.styles div { padding-top: 83.75796178% } | ||
|
||
:root.dark .img.styles { | ||
background-image: url(/images/annotations-styles-dark.png) | ||
} | ||
</style> |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
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,55 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from matplotlib.colors import to_rgba | ||
from typing import List, Tuple, Optional | ||
|
||
from .types import Color | ||
|
||
DEFAULT_1D_LINE_START = None | ||
DEFAULT_1D_LINE_END = None | ||
DEFAULT_LINE_COLOR = '#000000' | ||
DEFAULT_LINE_WIDTH = 1 | ||
|
||
@dataclass | ||
class HLine(): | ||
y: float | ||
x_start: Optional[float] = DEFAULT_1D_LINE_START | ||
x_end: Optional[float] = DEFAULT_1D_LINE_END | ||
line_color: Color = DEFAULT_LINE_COLOR | ||
line_width: int = DEFAULT_LINE_WIDTH | ||
|
||
def __post_init__(self): | ||
self.line_color = to_rgba(self.line_color) | ||
|
||
@dataclass | ||
class VLine(): | ||
x: float | ||
y_start: Optional[float] = DEFAULT_1D_LINE_START | ||
y_end: Optional[float] = DEFAULT_1D_LINE_END | ||
line_color: Color = DEFAULT_LINE_COLOR | ||
line_width: int = DEFAULT_LINE_WIDTH | ||
|
||
def __post_init__(self): | ||
self.line_color = to_rgba(self.line_color) | ||
|
||
@dataclass | ||
class Rect(): | ||
x_start: float | ||
x_end: float | ||
y_start: float | ||
y_end: float | ||
line_color: Color = DEFAULT_LINE_COLOR | ||
line_width: int = DEFAULT_LINE_WIDTH | ||
|
||
def __post_init__(self): | ||
self.line_color = to_rgba(self.line_color) | ||
|
||
@dataclass | ||
class Line(): | ||
vertices: List[Tuple[float]] | ||
line_color: Color = DEFAULT_LINE_COLOR | ||
line_width: int = DEFAULT_LINE_WIDTH | ||
|
||
def __post_init__(self): | ||
self.line_color = to_rgba(self.line_color) |
Oops, something went wrong.