Skip to content

Commit

Permalink
Merge pull request #196 from lsst/tickets/DM-47821
Browse files Browse the repository at this point in the history
Add utility functions for consistent multiband plot colors
  • Loading branch information
isullivan authored Nov 27, 2024
2 parents 8e2efdc + 6c3c530 commit e9fe97c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/changes/DM-47821.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``lsst.utils.plotting.get_multiband_plot_colors``, ``lsst.utils.plotting.get_multiband_plot_linestyles``, and ``lsst.utils.plotting.get_multiband_plot_symbols`` utilities for making consistent multi-band plots.
95 changes: 93 additions & 2 deletions python/lsst/utils/plotting/figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@

from __future__ import annotations

__all__ = ["make_figure"]
__all__ = [
"get_multiband_plot_colors",
"get_multiband_plot_symbols",
"get_multiband_plot_linestyles",
"make_figure",
]

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Dict

if TYPE_CHECKING:
from matplotlib.figure import Figure
Expand Down Expand Up @@ -55,3 +60,89 @@ def make_figure(**kwargs: Any) -> Figure:
FigureCanvasAgg(fig)

return fig


def get_multiband_plot_colors(dark_background: bool = False) -> Dict:
"""Get color mappings for multiband plots using SDSS filter names.
Notes
-----
From https://rtn-045.lsst.io/#colorblind-friendly-plots
Parameters
----------
dark_background : `bool`, optional
Use colors intended for a dark background.
Default colors are intended for a light background.
Returns
-------
plot_colors : `dict` of `str`
Mapping of the LSST bands to colors.
"""
plot_filter_colors_white_background = {
"u": "#0c71ff",
"g": "#49be61",
"r": "#c61c00",
"i": "#ffc200",
"z": "#f341a2",
"y": "#5d0000",
}
plot_filter_colors_black_background = {
"u": "#3eb7ff",
"g": "#30c39f",
"r": "#ff7e00",
"i": "#2af5ff",
"z": "#a7f9c1",
"y": "#fdc900",
}
if dark_background:
return plot_filter_colors_black_background
else:
return plot_filter_colors_white_background


def get_multiband_plot_symbols() -> Dict:
"""Get symbol mappings for multiband plots using SDSS filter names.
Notes
-----
From https://rtn-045.lsst.io/#colorblind-friendly-plots
Returns
-------
plot_symbols : `dict` of `str`
Mapping of the LSST bands to symbols.
"""
plot_symbols = {
"u": "o",
"g": "^",
"r": "v",
"i": "s",
"z": "*",
"y": "p",
}
return plot_symbols


def get_multiband_plot_linestyles() -> Dict:
"""Get line style mappings for multiband plots using SDSS filter names.
Notes
-----
From https://rtn-045.lsst.io/#colorblind-friendly-plots
Returns
-------
plot_linestyles : `dict` of `str`
Mapping of the LSST bands to line styles.
"""
plot_line_styles = {
"u": "--",
"g": ":",
"r": "-",
"i": "-.",
"z": (0, (3, 5, 1, 5, 1, 5)),
"y": (0, (3, 1, 1, 1)),
}
return plot_line_styles
24 changes: 22 additions & 2 deletions tests/test_matplotlib_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
import unittest

import lsst.utils.tests
from lsst.utils.plotting import make_figure
from lsst.utils.plotting import (
get_multiband_plot_colors,
get_multiband_plot_linestyles,
get_multiband_plot_symbols,
make_figure,
)

try:
from matplotlib.backends.backend_agg import FigureCanvasAgg
Expand All @@ -45,7 +50,22 @@ def testMakeFigure(self):
self.assertEqual(fig.get_figheight(), 6)
self.assertEqual(fig.get_dpi(), 300)

bands = ["u", "g", "r", "i", "z", "y"]

ax = fig.add_subplot(111)
ax.plot([0, 1], [0, 1], "r-")
for dark_background in True, False:
colors = get_multiband_plot_colors(dark_background=dark_background)
symbols = get_multiband_plot_symbols()
linestyles = get_multiband_plot_linestyles()

ax = fig.add_subplot(111)
for band in bands:
ax.plot(
[0, 1],
[0, 1],
c=colors[band],
marker=symbols[band],
linestyle=linestyles[band],
)

fig.savefig(tmpFile)

0 comments on commit e9fe97c

Please sign in to comment.