Callables
Many functions in Starplot allow you to pass a callable for creating dynamic styles and labels for objects. For example, when you plot stars you can optionally specify a callable for determining the size of each star. Starplot has a few basic callables built-in, but you can also create your own.
What's a Callable?
In Python, a "callable" is anything that can be "called" (e.g. a function or a class with __call__ implemented).
As a simple example, here's how you can pass a callable to Python's sorted function to sort a list of strings by their length:
>>> animals = ["elephant","cat", "dog", "tiger"]
>>> sorted(animals, key=lambda a: len(a))
['cat', 'dog', 'tiger', 'elephant']
In the example above, the value of key is the callable — in this case, a lambda function.
Here's another way to write the code above:
Size
starplot.callables.size_by_magnitude
size_by_magnitude(star: Star) -> float
Simple sizing by magnitude, using a step size of 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
star
|
Star
|
The Star instance to size |
required |
Source code in src/starplot/callables.py
starplot.callables.size_by_magnitude_galaxy
size_by_magnitude_galaxy(star: Star) -> float
Star sizer for galaxy plots that assumes only brighter stars will be plotted, so it uses smaller sizes overall.
This is the default star sizing function for GalaxyPlot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
star
|
Star
|
The Star instance to size |
required |
Source code in src/starplot/callables.py
starplot.callables.size_by_fov_factory
size_by_fov_factory(fov: float) -> Callable[[Star], float]
Returns a callable for sizing stars based on the field of view of an optic.
This is the default star sizing function for OpticPlot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fov
|
float
|
Field of view (in degrees) |
required |
Source code in src/starplot/callables.py
Color
starplot.callables.color_by_bv
color_by_bv(star: Star) -> str
Calculates color by the object's B-V index
Color hex values from: Mitchell Charity
Source code in src/starplot/callables.py
starplot.callables.color_by_bv_gradient
color_by_bv_gradient(star: Star) -> GradientStyle
Calculates a radial gradient by the object's B-V index, meant to resemble how a star looks through binoculars or a telescope: a bright core that gradually fades to fully transparent at the edge.
Uses the same base color as color_by_bv, lightened at the center of the gradient and faded to transparent at the outer edge.
Source code in src/starplot/callables.py
Labels
starplot.callables.floor_hours_label
Returns the floor of the value, with an 'h' appended to it.
Example: floor_hours_label(50) = '3h'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The value to label |
required |
Source code in src/starplot/callables.py
starplot.callables.rounded_degrees_label
Returns the rounded value with a degree symbol appended to it.
Example: rounded_degrees_label(50.45) = '50°'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The value to label |
required |
Source code in src/starplot/callables.py
starplot.callables.azimuth_with_cardinal_direction_label_factory
Returns a callable for labeling azimuth values. The callable returns the cardinal directions
(e.g. North, South, etc) where applicable, and returns the rounded azimuth value with a degree
symbol appended for other azimuths (e.g. '120°').
This is the default label function for azimuths on HorizonPlot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language for the cardinal directions |
required |
Source code in src/starplot/callables.py
Creating Your Own Callable
Let's say you wanted to create a plot where the stars brighter than magnitude 4 should be colored blue and stars dimmer than that should be colored red. Here's a way to do that with a custom callable:
# first we define the callable:
def color_by_mag(star: Star) -> str:
if star.magnitude <= 4:
return "#218fef"
else:
return "#d52727"
# then to use your callable:
p = MapPlot(...)
p.stars(
where=[_.magnitude < 12],
color_fn=color_by_mag,
)
Star, so you can reference various properties of stars in your callables. Similarly, every callable for a DSO is passed an instance of DSO.