steveberardi / starplot

✨ Star charts and maps in Python
https://starplot.dev
MIT License
17 stars 7 forks source link

Error when trying to set alpha value #43

Closed codingfar closed 5 months ago

codingfar commented 5 months ago

Using the code style.constellation_borders.alpha = 0 or, for example, style.bayer_labels.font_size = 15 works just fine, but inserting the code style.gridlines.alpha = 0 returns: ValueError: "PathStyle" object has no field "alpha". The same occurs if one tries, say, style.celestial_equator.alpha = 0. The code I was trying to use is copied below.


from starplot import PlotStyle, MapPlot, Projection

style = PlotStyle()
style.bayer_labels.font_name = "Times New Roman"
style.bayer_labels.font_size = 15
style.constellation_borders.alpha = 0
style.milky_way.alpha = 0
style.gridlines.alpha = 0

p = MapPlot(
    projection=Projection.MERCATOR,
    ra_min=4.6,
    ra_max=6.4,
    dec_min=-16,
    dec_max=23.6,
    limiting_magnitude=6.0,
    style=style,
    resolution=3600,
)

p.export("test.png", padding=0.5)

Alternatively, trying to create a style using a YAML file did not work. I tried the following code (adapted from the "More Complex Example" on the website):

from starplot import PlotStyle, MapPlot, Projection
from starplot.styles import LineStyle, ObjectStyle, PathStyle

style = PlotStyle.load_from_file("/Users/josephcaruana/Downloads/check/style.yml")

p = MapPlot(
    projection=Projection.MERCATOR,
    ra_min=4.6,
    ra_max=6.5,
    dec_min=-16,
    dec_max=23.6,
    style=style,
)

p.export("figure.png", padding=0.5)

I used the following YAML file:

# style.yml

# hide the constellation labels/lines:
constellation:
  label:
    visible: false
  line:
    visible: false

# make the Milky Way gray
milky_way:
  visible: false

# change the color of star labels to blue and
# and change their symbol from dots to stars
star:
  label:
    font_color: '#0e69b8'
  marker:
    symbol: '*'

# make nebulas green and their markers diamonds
dso_nebula:
  marker:
    color: green
    symbol: D

However, although this did not return any errors, none of the chosen style options were implemented in the chart that was produced.

steveberardi commented 5 months ago

Hi, thanks for reporting these issues! It seems the documentation is not very clear that some objects have a PathStyle, some have a LineStyle, and some have a wrapper that includes something like a LineStyle AND a LabelStyle — I implemented it this way to keep the styling flexible. I’ll try to make this more clear in the documentation.

Anyway, I updated your code and added a few comments to get your first example working:

from starplot import PlotStyle, MapPlot, Projection

style = PlotStyle()
style.bayer_labels.font_name = "Times New Roman"
style.bayer_labels.font_size = 15

# since the constellation borders are a LineStyle, you can set the alpha directly. 
# Currently they have no labels, so I didn't wrap the border style in a PathStyle 
# like the Celestial Equator (which DOES have an optional label)
style.constellation_borders.alpha = 0
style.milky_way.alpha = 0

# Gridlines have labels on the plot axes, so gridlines are actually a PathStyle 
# (which has a "line" and a "label"). So, to set ONLY the alpha on the lines, use:
style.gridlines.line.alpha = 0

# for the labels you'd do:
# style.gridlines.label.font_alpha = 0

# below code is unchanged:

p = MapPlot(
    projection=Projection.MERCATOR,
    ra_min=4.6,
    ra_max=6.4,
    dec_min=-16,
    dec_max=23.6,
    limiting_magnitude=6.0,
    style=style,
    resolution=3600,
)

p.export("test.png", padding=0.5)

With those changes, I get this:

01

Also, if you want to hide certain objects, I recommend setting the visible style property to "false" instead of alpha to 0 because Starplot will exit functions early (and thus, speed up total run time) when an object's visibility is set to false.

In your second example that loads a style from a YAML file, looks like this is caused by a bug in the PlotStyle’s load_from_file function. I just released a new version of Starplot, 0.7.1, that fixes this :)

With the bug fix, I get this when running your code:

02

Which looks correct, except the star symbols are dots instead of stars. I’ll look into that…

Thanks again for reporting these issues! Starplot is still very much a work in progress, so I also appreciate your patience and understanding.

steveberardi commented 5 months ago

I figured out why the star symbols are being plotted as dots instead of what's in the style, so I'll fix that bug soon too. I'll keep this issue open until then.