Skip to content
Snippets Groups Projects
Commit 6d7935d4 authored by Germain Clavier's avatar Germain Clavier
Browse files

Changed OldStyle.py to more convenien Old.mplstyle. Modified qp so that it...

Changed OldStyle.py to more convenien Old.mplstyle. Modified qp so that it looks for it as a matplotlib style. Still need to remove all instances of color handling.
parent 442e987a
No related branches found
No related tags found
No related merge requests found
# I don't like other matplotlib styles so I made this one with an old school
# journal feeling that I prefer. It is intended to make most figures readable
# even printed in black and white. Put this file in your matplotlib config file
# directory in the stylelib directory, use pyplot.style.use('Old') in your
# script and you're done.
# See https://matplotlib.org/stable/tutorials/introductory/customizing.html
# for more info.
# You can use a symbolic link if you forked the git repo and want to modify or
# update it regularly.
font.family : Latin Modern Math
font.weight : normal
font.size : 20
font.variant : small-caps # Unfortunately, this does not work yet with matplotlib
axes.linewidth : 1
lines.linewidth : 3
axes.labelsize : 20
legend.fontsize : 20
xtick.major.size : 15
xtick.minor.visible : True
xtick.minor.size : 7
xtick.labelsize : 20
ytick.labelsize : 20
xtick.direction : in
xtick.top : True
ytick.direction : in
ytick.right : True
ytick.major.size : 15
ytick.minor.visible : True
ytick.minor.size : 7
xtick.major.width : 1
ytick.major.width : 1
xtick.minor.width : 1
ytick.minor.width : 1
lines.markerfacecolor : white
lines.markeredgecolor : black
lines.markersize : 10
axes.grid : True
axes.prop_cycle: cycler(color=['000000', '888888', 'CCCCCC']) * (cycler(linestyle=['-', '--', '-.', ':']) + cycler(marker=['o', 's', 'v', '^']))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
An old scitific journal style inspired plotting configuration file
'''
import numpy as np
import re
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import sys
plt.rc('axes', linewidth=2)
label_fontsize = 20
ticks_fontsize = 20
linewidth = 3
axes_linewidth = 1
major_tick_length = 15
minor_tick_length = major_tick_length//2
no_tick = 0
# print(plt.rcParams.keys())
# sys.exit()
plt.rcParams['font.family'] = 'Latin Modern Math'
plt.rcParams['font.weight'] = 'normal'
plt.rcParams['font.variant'] = 'small-caps' # Unfortunately, this does not work yet with matplotlib
plt.rcParams['axes.linewidth'] = axes_linewidth
plt.rcParams['lines.linewidth'] = linewidth
plt.rcParams['axes.labelsize'] = label_fontsize
plt.rcParams['legend.fontsize'] = label_fontsize
plt.rcParams['xtick.major.size'] = major_tick_length
plt.rcParams['xtick.minor.visible'] = True
plt.rcParams['xtick.minor.size'] = minor_tick_length
plt.rcParams['xtick.labelsize'] = ticks_fontsize
plt.rcParams['ytick.labelsize'] = ticks_fontsize
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['xtick.top'] = True
plt.rcParams['ytick.direction'] = 'in'
plt.rcParams['ytick.right'] = True
plt.rcParams['ytick.major.size'] = major_tick_length
plt.rcParams['ytick.minor.visible'] = True
plt.rcParams['ytick.minor.size'] = minor_tick_length
plt.rcParams['xtick.major.width'] = axes_linewidth
plt.rcParams['ytick.major.width'] = axes_linewidth
plt.rcParams['xtick.minor.width'] = axes_linewidth
plt.rcParams['ytick.minor.width'] = axes_linewidth
plt.rcParams['lines.markerfacecolor'] = 'white'
plt.rcParams['lines.markeredgecolor'] = 'black'
nlinestyle = 4
linestyle_list = ['-', '--', '-.', ':']
color_list = ['#000000', '#888888', '#CCCCCC']
marker_list = ['o', 's', 'v', '^']
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=color_list) * (plt.cycler(linestyle=linestyle_list) + plt.cycler(marker=marker_list))
##### SOME EXAMPLES FOR TESTING PURPOSE ####
##### Plotting linear data
# npoints = 10
# for i in range(12):
# data = np.linspace(0, 1, 10)
# plt.plot((i+1)*data, label=" ".join(['data', str(i+1)]))
# ax = plt.gca()
# ax.legend(frameon=False)
# ax.set_xlabel(r"PIF ($\int_{-\infty}^{3} \frac{x}{l_0}$)")
# ax.set_ylabel("POUET")
##### Show result
plt.show()
......@@ -10,7 +10,6 @@ import os
import sys
import re
import GCcolors
from itertools import cycle
import numpy as np
......@@ -18,8 +17,6 @@ from scipy.signal import savgol_filter
import matplotlib as mpl
from matplotlib import pyplot as plt
# plt.rcParams.update({'lines.markeredgewidth': 4})
plt.rcParams.update({'font.size': 22, 'lines.linewidth': 3.})
def read_columns(fle, columns_list, fmt, skip, xsca, ysca, sg):
......@@ -60,7 +57,7 @@ def read_columns(fle, columns_list, fmt, skip, xsca, ysca, sg):
return output
def plot_sep_col(data, colors, fmt, is_log, legend, sy):
def plot_sep_col(data, fmt, is_log, legend, sy, hline, vline):
'''
Makes separated ordered columns
wise: all 1:2 together, all 1:3 together
......@@ -87,10 +84,6 @@ def plot_sep_col(data, colors, fmt, is_log, legend, sy):
break
plot_nb = len(columns)
if sy:
symbols = {'linestyle': "None", 'marker': "o"}
else:
symbols = {'linestyle': '-', 'marker': "None"}
if plot_nb % 3 == 0:
fig, axes = plt.subplots(plot_nb//3, 3, sharex=True, figsize=(20, 10))
elif plot_nb % 2 == 0:
......@@ -98,10 +91,8 @@ def plot_sep_col(data, colors, fmt, is_log, legend, sy):
else:
fig, axes = plt.subplots(plot_nb, 1, sharex=True, figsize=(20, 10))
for pair, ax in zip(columns, axes.flatten()):
color_iter = cycle(colors)
suf = ":".join([str(pair[0]), str(pair[1])])
for d in data:
color = next(color_iter)
label = d[0]
for subdata in d[1:]:
x = subdata[0]
......@@ -113,19 +104,18 @@ def plot_sep_col(data, colors, fmt, is_log, legend, sy):
y_data = subdata[3]
if fmt == 'xy':
ax.plot(x_data, y_data,
label=lab, color=color,
**symbols
label=lab,
)
elif fmt == 'xydy':
dy = subdata[4]
plt.plot(x_data, y_data,
label=lab, color=color
label=lab,
)
up = y_data+dy
down = y_data-dy
plt.fill_between(
x_data, up, down,
alpha=0.2, edgecolor=color, facecolor=color,
alpha=0.2,
linewidth=0
)
elif fmt == 'xydx':
......@@ -133,7 +123,7 @@ def plot_sep_col(data, colors, fmt, is_log, legend, sy):
ax.errorbar(x_data, y_data,
xerr=dx,
capsize=4, capthick=2,
label=lab, color=color
label=lab,
)
elif fmt == 'xydxdy':
dx = subdata[4]
......@@ -141,12 +131,16 @@ def plot_sep_col(data, colors, fmt, is_log, legend, sy):
ax.errorbar(x_data, y_data,
xerr=dx, yerr=dy,
capsize=4, capthick=2,
label=lab, color=color
label=lab,
)
if is_log[0]:
ax.set_xscale('log')
if is_log[1]:
ax.set_yscale('log')
for h in hline:
plt.hlines(y=h, xmin=-np.inf, xmax=np.inf, linestyle='--')
for v in vline:
plt.vlines(x=v, ymin=-np.inf, ymax=np.inf, linestyle='--')
if legend:
ncol = max([1, len(data)//3])
ax.legend(loc='upper left', ncol=ncol)
......@@ -155,12 +149,8 @@ def plot_sep_col(data, colors, fmt, is_log, legend, sy):
return [fig, axes]
def plot_sep_fil(data, colors, fmt, is_log, legend, sy):
def plot_sep_fil(data, fmt, is_log, legend, sy, hline, vline):
plot_nb = len(data)
if sy:
symbols = {'linestyle': "None", 'marker': "o"}
else:
symbols = {'linestyle': '-', 'marker': "None"}
if plot_nb % 3 == 0:
fig, axes = plt.subplots(plot_nb//3, 3, sharex=True, figsize=(20, 10))
elif plot_nb % 2 == 0:
......@@ -168,9 +158,7 @@ def plot_sep_fil(data, colors, fmt, is_log, legend, sy):
else:
fig, axes = plt.subplots(plot_nb, 1, sharex=True, figsize=(20, 10))
for d, ax in zip(data, axes.flatten()):
color_iter = cycle(colors)
suf = d[0]
color = next(color_iter)
for subdata in d[1:]:
x = subdata[0]
y = subdata[1]
......@@ -179,19 +167,18 @@ def plot_sep_fil(data, colors, fmt, is_log, legend, sy):
y_data = subdata[3]
if fmt == 'xy':
ax.plot(x_data, y_data,
label=lab, color=color,
**symbols
label=lab,
)
elif fmt == 'xydy':
dy = subdata[4]
plt.plot(x_data, y_data,
label=lab, color=color
label=lab,
)
up = y_data+dy
down = y_data-dy
plt.fill_between(
x_data, up, down,
alpha=0.2, edgecolor=color, facecolor=color,
alpha=0.2,
linewidth=0
)
elif fmt == 'xydx':
......@@ -199,7 +186,7 @@ def plot_sep_fil(data, colors, fmt, is_log, legend, sy):
ax.errorbar(x_data, y_data,
xerr=dx,
capsize=4, capthick=2,
label=lab, color=color
label=lab,
)
elif fmt == 'xydxdy':
dx = subdata[4]
......@@ -207,12 +194,16 @@ def plot_sep_fil(data, colors, fmt, is_log, legend, sy):
ax.errorbar(x_data, y_data,
xerr=dx, yerr=dy,
capsize=4, capthick=2,
label=lab, color=color
label=lab,
)
if is_log[0]:
ax.set_xscale('log')
if is_log[1]:
ax.set_yscale('log')
for h in hline:
plt.hlines(y=h, xmin=-np.inf, xmax=np.inf, linestyle='--')
for v in vline:
plt.vlines(x=v, ymin=-np.inf, ymax=np.inf, linestyle='--')
if legend:
ncol = max([1, len(data)//3])
ax.legend(loc='upper left', ncol=ncol)
......@@ -221,16 +212,16 @@ def plot_sep_fil(data, colors, fmt, is_log, legend, sy):
return [fig, axes]
def plot_data(data, colors, fmt, is_log, legend, names, sy):
color_iter = cycle(colors)
def plot_data(data, colors, fmt, is_log, legend, names, sy, hline, vline):
# color_iter = cycle(colors)
plt.figure(figsize=(40, 30))
namelist = iter(names)
if sy:
symbols = {'linestyle': "None", 'marker': "o"}
else:
symbols = {'linestyle': '-', 'marker': "None"}
ax = plt.gca()
for h in hline:
ax.axhline(y=float(h), linestyle='--')
for v in vline:
ax.axvline(x=float(v), ymin=0, ymax=1, linestyle='--')
for d in data:
color = next(color_iter)
try:
label = next(namelist)
except StopIteration:
......@@ -248,19 +239,16 @@ def plot_data(data, colors, fmt, is_log, legend, names, sy):
y_data = subdata[3]
if fmt == 'xy':
plt.plot(x_data, y_data,
label=lab, color=color,
**symbols
)
label=lab,)
elif fmt == 'xydy':
dy = subdata[4]
plt.plot(x_data, y_data,
label=lab, color=color
)
label=lab,)
up = y_data+dy
down = y_data-dy
plt.fill_between(
x_data, up, down,
alpha=0.2, edgecolor=color, facecolor=color,
alpha=0.2,
linewidth=0
)
elif fmt == 'xydx':
......@@ -268,7 +256,7 @@ def plot_data(data, colors, fmt, is_log, legend, names, sy):
plt.errorbar(x_data, y_data,
xerr=dx,
capsize=4, capthick=2,
label=lab, color=color
label=lab,
)
elif fmt == 'xydxdy':
dx = subdata[4]
......@@ -276,7 +264,7 @@ def plot_data(data, colors, fmt, is_log, legend, names, sy):
plt.errorbar(x_data, y_data,
xerr=dx, yerr=dy,
capsize=4, capthick=2,
label=lab, color=color
label=lab,
)
if is_log[0]:
plt.xscale('log')
......@@ -310,7 +298,7 @@ def main():
help='File name followed by columns and optionally lines to skip.')
parser.add_argument('-n', '--names', dest='names',
nargs='+', action='append',
default=[''],
default=[],
help='Names for the legend.')
parser.add_argument('-l', '--loglog', dest='is_log_xy',
action='store_true',
......@@ -341,6 +329,12 @@ def main():
parser.add_argument('-sg', '--savitsky-golay',
dest='SG', type=int, default=0,
help='Savitsky-golay window (default=0=none)')
parser.add_argument('--hline', dest='hline', nargs='+',
action='append',
help='Coordinate of horizontal lines to add')
parser.add_argument('--vline', dest='vline', nargs='+',
action='append',
help='Coordinate of vertical lines to add')
args = parser.parse_args()
......@@ -354,6 +348,8 @@ def main():
ylab = args.ylab
xsca = args.xsca
ysca = args.ysca
hline = args.hline[0] if args.hline else []
vline = args.vline[0] if args.vline else []
is_log_xy = args.is_log_xy
is_log_x = args.is_log_x
......@@ -392,6 +388,15 @@ def main():
else:
sys.exit('Unknown fmt in -fmt option.')
try:
plt.style.use('Old')
except Exception:
pass
# linestyle_list = ['-', '--', '-.', ':']
# color_list = ['#000000', '#888888', '#CCCCCC']
# color_list = ['#000000']
# marker_list = ['o', 's', 'v', '^']
# plt.rcParams['axes.prop_cycle'] = plt.cycler(color=color_list) * (plt.cycler(linestyle=linestyle_list) + plt.cycler(marker=marker_list))
regex_sk = re.compile(r'^\d+$')
if noGC:
colors = ['black', 'darkred', 'red',
......@@ -410,7 +415,7 @@ def main():
color = (1-x)*col1 + x*col2
colors.append(color)
else:
colors = GCcolors.color_list
colors = ['blue', 'red', 'green'] # GCcolors.color_list
columns_list = []
data = []
......@@ -430,11 +435,11 @@ def main():
data.append(read_columns(fle, columns_list, fmt, skip, xsca, ysca, sg))
if sep_col:
fig, axes = plot_sep_col(data, colors, fmt, is_log, legend, sy)
fig, axes = plot_sep_col(data, colors, fmt, is_log, legend, sy, hline, vline)
elif sep_files:
fig, axes = plot_sep_fil(data, colors, fmt, is_log, legend, sy)
fig, axes = plot_sep_fil(data, colors, fmt, is_log, legend, sy, hline, vline)
else:
plot_data(data, colors, fmt, is_log, legend, names, sy)
plot_data(data, colors, fmt, is_log, legend, names, sy, hline, vline)
plt.xlabel(xlab)
plt.ylabel(ylab)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment