Skip to content

Commit

Permalink
add plotting styles
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-richard committed Feb 9, 2024
1 parent 7f36627 commit 13edf58
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 9 deletions.
192 changes: 183 additions & 9 deletions pyrfu/plot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Built-in imports
import os
import shutil
import warnings

# 3rd party imports
import matplotlib.pyplot as plt
import cycler
import matplotlib as mpl
from matplotlib import style

# Local imports
from .add_position import add_position
from .annotate_heatmap import annotate_heatmap
from .colorbar import colorbar
Expand All @@ -14,8 +22,6 @@
from .plot_ang_ang import plot_ang_ang
from .plot_clines import plot_clines
from .plot_heatmap import plot_heatmap

# Local imports
from .plot_line import plot_line
from .plot_magnetosphere import plot_magnetosphere
from .plot_projection import plot_projection
Expand All @@ -34,12 +40,6 @@
__status__ = "Prototype"


# Setup plotting style
plt.style.use("classic")
plt.rcParams["figure.facecolor"] = "1"
plt.rcParams["mathtext.sf"] = "sans"
plt.rcParams["mathtext.fontset"] = "dejavusans"

__all__ = [
"add_position",
"annotate_heatmap",
Expand All @@ -58,6 +58,180 @@
"plot_sitl_overview",
"plot_spectr",
"plot_surf",
"set_color_cycle",
"span_tint",
"use_pyrfu_style",
"zoom",
]

stylesheets = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stylesheets")
style.core.USER_LIBRARY_PATHS.append(stylesheets)
style.core.reload_library()

EXTRA_COLORS = {
"pyrfu:bg": "#eeeeee",
"pyrfu:fg": "#444444",
"pyrfu:bgAlt": "#e4e4e4",
"pyrfu:red": "#af0000",
"pyrfu:green": "#008700",
"pyrfu:blue": "#005f87",
"pyrfu:yellow": "#afaf00",
"pyrfu:orange": "#d75f00",
"pyrfu:pink": "#d70087",
"pyrfu:purple": "#8700af",
"pyrfu:lightblue": "#0087af",
"pyrfu:olive": "#5f7800",
"on:bg": "#1b2b34",
"on:fg": "#cdd3de",
"on:bgAlt": "#343d46",
"on:fgAlt": "#d8dee9",
"on:red": "#ec5f67",
"on:orange": "#f99157",
"on:yellow": "#fac863",
"on:green": "#99c794",
"on:cyan": "#5fb3b3",
"on:blue": "#6699cc",
"on:pink": "#c594c5",
"on:brown": "#ab7967",
"series:cyan": "#54c9d1",
"series:orange": "#eca89a",
"series:blue": "#95bced",
"series:olive": "#ceb776",
"series:purple": "#d3a9ea",
"series:green": "#9bc57f",
"series:pink": "#f0a1ca",
"series:turquoise": "#5fcbaa",
"transparent": "#ffffff00",
}

mpl.colors.EXTRA_COLORS = EXTRA_COLORS
mpl.colors.colorConverter.colors.update(EXTRA_COLORS)
mpl.colormaps.register(
name="bird",
cmap=mpl.colors.LinearSegmentedColormap.from_list(
"bird",
[
(0.0592, 0.3599, 0.8684),
(0.078, 0.5041, 0.8385),
(0.0232, 0.6419, 0.7914),
(0.1802, 0.7178, 0.6425),
(0.5301, 0.7492, 0.4662),
(0.8186, 0.7328, 0.3499),
(0.9956, 0.7862, 0.1968),
(0.9764, 0.9832, 0.0539),
],
N=2560,
),
)


def set_color_cycle(pal=None):
r"""Sets color cycle.
Parameters
----------
pal : {"Pyrfu", "Oceanic", "Tab", None}. Optional
The palette to use. "Tab" provides the default matplotlib palette. Default is
None (resets to default palette).
"""

if pal.lower() == "pyrfu":
colors = [
"pyrfu:blue",
"pyrfu:green",
"pyrfu:red",
"pyrfu:fg",
"pyrfu:orange",
"pyrfu:purple",
"pyrfu:yellow",
"pyrfu:lightblue",
"pyrfu:olive",
]
elif pal.lower() == "oceanic":
colors = [
"on:green",
"on:red",
"on:blue",
"on:cyan",
"on:orange",
"on:pink",
"on:yellow",
]
elif pal.lower() == "tab" or pal.lower() == "tableau" or pal.lower() == "mpl":
colors = [
"tab:blue",
"tab:orange",
"tab:green",
"tab:red",
"tab:purple",
"tab:brown",
"tab:pink",
"tab:gray",
"tab:olive",
"tab:cyan",
]
else:
colors = [
"series:cyan",
"series:orange",
"series:blue",
"series:olive",
"series:purple",
"series:green",
"series:pink",
]

mpl.rcParams["axes.prop_cycle"] = cycler.cycler(color=colors)


def use_pyrfu_style(
name="classic", color_cycle="pyrfu", fancy_legend=False, usetex=False
):
r"""Setup plot style.
Parameters
----------
name : str, Optional
Name of the style sheet. Default is "classic" (see also
https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html).
color_cycle : str, Optional
Name of the color cycle to use. Default is "pyrfu".
fancy_legend : bool, Optional
Use matplotlib's fancy legend frame. Default is False.
usetex : bool, Optional
Use LaTeX installation to set text. Default is False.
If no LaTeX installation is found, this package will fallback to usetex=False.
"""

if usetex:
if (
shutil.which("pdflatex") is None
or shutil.which("dvipng") is None
or shutil.which("gs") is None
):
warnings.warn(
"No LaTeX installation found pyrfu.plot is falling back to usetex=False"
)
usetex = False

style.use(name)
set_color_cycle(color_cycle)
mpl.rcParams["xtick.minor.visible"] = True
mpl.rcParams["ytick.minor.visible"] = True
if not usetex:
mpl.rcParams["text.usetex"] = False
mpl.rcParams["mathtext.sf"] = "sans"
mpl.rcParams["mathtext.fontset"] = "dejavusans"
else:
mpl.rcParams["text.latex.preamble"] = "\n".join(
[
r"\usepackage{amsmath}",
r"\usepackage{physics}",
r"\usepackage{siunitx}",
r"\usepackage{bm}",
]
)
if not fancy_legend:
mpl.rcParams["legend.frameon"] = False
mpl.rcParams["legend.fancybox"] = False
mpl.rcParams["legend.framealpha"] = 0.75
74 changes: 74 additions & 0 deletions pyrfu/stylesheets/aps.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## *************************************************************************************
## * Matplotlib style sheet for APS publications *
## author : Louis Richard
## email : [email protected]
## *************************************************************************************

## Fonts
font.family: serif
font.style: normal
font.variant: normal
font.weight: normal
font.stretch: normal
font.size: 8.0
#font.serif: Times, Computer Modern Roman
#font.sans-serif: Helvetica, Computer Modern Sans serif

## LaTeX
#text.usetex: True
#text.latex.preamble: \usepackage{bm}
pgf.texsystem: xelatex

## Axes
axes.labelsize: 8
axes.linewidth: .5
axes.xmargin: 0
axes.ymargin: 0

## Save
savefig.dpi: 600
savefig.format: eps
# savefig.bbox: tight

## Legend
legend.loc: best
legend.frameon: False # if True, draw the legend on a background patch
legend.framealpha: 0.8 # legend patch transparency
legend.facecolor: inherit # inherit from axes.facecolor; or color spec
legend.edgecolor: 0.8 # background patch boundary color
legend.fancybox: False # if True, use a rectangle box for the legend background
legend.shadow: False # if True, give background a shadow effect
legend.numpoints: 1 # the number of marker points in the legend line
legend.scatterpoints: 1 # number of scatter points
legend.markerscale: 0.9 # the relative size of legend markers vs. original
legend.fontsize: 8
legend.title_fontsize: None # None sets to the same as the default axes.

## Dimensions as fraction of fontsize:
legend.borderpad: 0.2 # border whitespace
legend.labelspacing: 0.2 # the vertical space between the legend entries
legend.handlelength: 0.1 # the length of the legend lines
#legend.handleheight: 0.4 # the height of the legend handle
legend.handletextpad: 0.1 # the space between the legend line and legend text
legend.borderaxespad: 0.4 # the border between the axes and legend edge
legend.columnspacing: 0.8 # column separation

## Ticks
xtick.labelsize: 8
xtick.major.size: 2
xtick.minor.size: 1.2
xtick.direction: in
xtick.top: True

ytick.labelsize: 8
ytick.major.size: 2
ytick.minor.size: 1.2
ytick.direction : in
ytick.right: True

## Lines
lines.linewidth: 0.6
lines.markersize: 3

## Errorbars
errorbar.capsize: 2

0 comments on commit 13edf58

Please sign in to comment.