6 - Selecting Objects to Plot


Tutorial - Selecting Objects

When plotting stars or deep sky objects (DSOs), you may want to limit the plotted objects by more than just a limiting magnitude. Starplot provides a way to filter stars/DSOs by using expressions. This allows you to be very specific about which objects to plot, and it also gives you a way to style objects differently (e.g. if you want to style very bright stars differently than dim stars).

To select objects to plot, you pass a list of expressions using the where keyword argument. For example, here's how you'd plot all stars that have a magnitude less than 6 and a B-V index of more than 0.4:

p.stars(
    where=[
        Star.magnitude < 6,
        Star.bv > 0.4,
    ],
)
When passing the where kwarg, the mag kwarg will be ignored, and only the stars/DSOs that satisfy ALL the where conditions will be plotted. You can reference any field on the model in expressions, and there are also a few functions available for determining if a field is null or is in a list of values. For complete details about selecting objects, see the reference page.

Here's some code that uses the where kwarg extensively to produce the image above:

from starplot import MapPlot, Projection, DSO, Star
from starplot.styles import PlotStyle, extensions

style = PlotStyle().extend(
    extensions.ANTIQUE,
    extensions.MAP,
    {
        "bayer_labels": {
            "font_name": "GFS Didot",
            "font_size": 7,
        },
        "dso_open_cluster": {
            "marker": {
                "size": 17,
            },
        },
    },
)
p = MapPlot(
    projection=Projection.MILLER,
    ra_min=15.6,
    ra_max=19.8,
    dec_min=-45.6,
    dec_max=-3,
    style=style,
    resolution=2000,
)
p.constellations()

p.stars(
    where=[Star.magnitude <= 3],  # select the brightest stars
    style__marker__size=76,  # make them bigger
    style__marker__symbol="star_8",  # use an 8-pointed star for bright star markers
    style__marker__zorder=200,
)
p.stars(
    where=[
        Star.magnitude > 3,  # select the dimmer stars
        Star.magnitude < 11,
    ],
    style__marker__size=12,
    bayer_labels=True,
    catalog="big-sky-mag11",
)

p.nebula(
    where=[
        # select DSOs which have no defined magnitude or less than 12
        DSO.magnitude.is_null()
        | (DSO.magnitude < 12),
    ],
    true_size=True,  # plot nebula as their true size
)
p.open_clusters(
    where=[
        DSO.magnitude.is_null() | (DSO.magnitude < 12),
    ],
    true_size=False,
)
p.constellation_borders()
p.ecliptic()
p.celestial_equator()
p.milky_way()
p.export("tutorial_06.png", padding=0.08)

In the next section, we'll learn how to lookup objects to create plots...