Provides a custom plotting style for matplotlib and other libraries building on it like pandas or seaborn. The plotting style is set to aproximately the styles described in our wiki: https://wiki.cpi.imtek.uni-freiburg.de/ScientificWriting
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

67 lines
2.3 KiB

from pathlib import Path
file_custom_edits = Path("./custom cpi styles.txt")
file_defaults = Path("./default.mplstyle")
file_cpi_style = Path("./cpi.mplstyle")
cpi_style_header = """
### CPI matplotlib plotting style
# the plotting styles are set to aproximately the styles described in
# https://wiki.cpi.imtek.uni-freiburg.de/ScientificWriting
# in the file `140109-scientific-writing-06-figures-and-figure-captions.pptx`
# usage with matplotlib / pyplot:
# > from matplotlib import pyplot
# > pyplot.style.use('./cpi.mplstyle')
# usage with other libraries based on matplotlib, like searborn:
# > from matplotlib import pyplot
# > import seaborn
# > pyplot.style.use('./cpi.mplstyle')
#
# be aware that the set* functions of seaborn (like set_theme) might
# mess with the matplotlib style.
#
# to use the seaborn.set_palette() function to change line colors seems
# to be fine
# to use this style as your default matplotlib style, have a look at
# https://matplotlib.org/stable/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files
"""
def get_custom_edits(filepath=file_custom_edits):
with filepath.open("r") as fh:
lines = (line.strip() for line in fh)
not_empty = (line for line in lines if line)
with_data = (line for line in not_empty if ":" in line)
raw_data = (line.split(":", 1) for line in with_data)
data = ((k.strip(), v.strip()) for k, v in raw_data)
return dict(sorted(data))
custom_edits = get_custom_edits(file_custom_edits)
key_max_length = 1 + max(len(k) for k in custom_edits)
with open(file_cpi_style, "w") as fw:
fw.write(cpi_style_header)
for key, value in custom_edits.items():
line = f"{key:<{key_max_length}}: {value}"
fw.write(f"{line}\n")
fw.write(f"\n\n\n")
fw.write(f"### Following are the default matplotlib settings\n")
fw.write(f"### Custom styles are commented out\n\n")
with open(file_defaults, "r") as fr:
for line in fr:
try:
key = line.split(":")[0]
if key.strip() in custom_edits:
line = f"#{line}"
except ValueError():
pass
fw.write(line)