From c029c563255e2f6686c252fa72a7db639e2db294 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Sat, 4 Jan 2020 22:52:58 +0100 Subject: [PATCH] [plotstyle] new sketch_style(), plain_style() and plot_style() --- plotstyle.py | 103 ++++++++++++++++++++----------- regression/lecture/derivative.py | 2 + regression/lecture/gradient.py | 2 + 3 files changed, 71 insertions(+), 36 deletions(-) diff --git a/plotstyle.py b/plotstyle.py index aeec502..04e821e 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -21,6 +21,9 @@ colors = { 'blue': '#0000CC' } +# factor for scaling widths of bars in a bar plot: +bar_fac = 1.0 + def cm_size(*args): """ Convert dimensions from cm to inch. @@ -180,7 +183,7 @@ def __axis_label(label, unit=None): return '%s [%s]' % (label, unit) -def __set_xlabel(ax, label, unit=None, **kwargs): +def set_xlabel(ax, label, unit=None, **kwargs): """ Format the xlabel from a label and an unit. Uses the __axis_label() function to format the axis label. @@ -197,7 +200,7 @@ def __set_xlabel(ax, label, unit=None, **kwargs): ax.set_xlabel_orig(__axis_label(label, unit), **kwargs) -def __set_ylabel(ax, label, unit=None, **kwargs): +def set_ylabel(ax, label, unit=None, **kwargs): """ Format the ylabel from a label and an unit. Uses the __axis_label() function to format the axis label. @@ -214,7 +217,7 @@ def __set_ylabel(ax, label, unit=None, **kwargs): ax.set_ylabel_orig(__axis_label(label, unit), **kwargs) -def __set_zlabel(ax, label, unit=None, **kwargs): +def set_zlabel(ax, label, unit=None, **kwargs): """ Format the zlabel from a label and an unit. Uses the __axis_label() function to format the axis label. @@ -231,47 +234,75 @@ def __set_zlabel(ax, label, unit=None, **kwargs): ax.set_zlabel_orig(__axis_label(label, unit), **kwargs) -# overwrite axes constructor: -mpl.axes.Subplot.__init__orig = mpl.axes.Subplot.__init__ -mpl.axes.Subplot.__init__ = __axes__init__ -mpl.axes.Axes.show_spines = show_spines - -# overwrite axes set_[xyz]label() member functions: -mpl.axes.Axes.set_xlabel_orig = mpl.axes.Axes.set_xlabel -mpl.axes.Axes.set_xlabel = __set_xlabel -mpl.axes.Axes.set_ylabel_orig = mpl.axes.Axes.set_ylabel -mpl.axes.Axes.set_ylabel = __set_ylabel -Axes3D.set_zlabel_orig = Axes3D.set_zlabel -Axes3D.set_zlabel = __set_zlabel +def common_format(): + """ Set some rc parameter. + """ + mpl.rcParams['figure.figsize'] = cm_size(figure_width, figure_height) + mpl.rcParams['figure.subplot.left'] = 5.5*0.6*mpl.rcParams['font.size']/cm_size(figure_width)/ppi + mpl.rcParams['figure.subplot.right'] = 1.0 - 0.5*0.6*mpl.rcParams['font.size']/cm_size(figure_width)/ppi + mpl.rcParams['figure.subplot.bottom'] = 2.8*mpl.rcParams['font.size']/cm_size(figure_height)/ppi + mpl.rcParams['figure.subplot.top'] = 1.0 - 0.5*mpl.rcParams['font.size']/cm_size(figure_height)/ppi + mpl.rcParams['figure.subplot.wspace'] = 0.4 + mpl.rcParams['figure.subplot.hspace'] = 0.6 + mpl.rcParams['figure.facecolor'] = 'white' + mpl.rcParams['xtick.direction'] = 'out' + mpl.rcParams['ytick.direction'] = 'out' + mpl.rcParams['xtick.major.width'] = 1.25 + mpl.rcParams['ytick.major.width'] = 1.25 + # overwrite axes constructor: + if not hasattr(mpl.axes.Subplot, '__init__orig'): + mpl.axes.Subplot.__init__orig = mpl.axes.Subplot.__init__ + mpl.axes.Subplot.__init__ = __axes__init__ + mpl.axes.Axes.show_spines = show_spines + # overwrite axes set_[xyz]label() member functions: + if not hasattr(mpl.axes.Axes, 'set_xlabel_orig'): + mpl.axes.Axes.set_xlabel_orig = mpl.axes.Axes.set_xlabel + mpl.axes.Axes.set_xlabel = set_xlabel + if not hasattr(mpl.axes.Axes, 'set_ylabel_orig'): + mpl.axes.Axes.set_ylabel_orig = mpl.axes.Axes.set_ylabel + mpl.axes.Axes.set_ylabel = set_ylabel + if not hasattr(Axes3D, 'set_zlabel_orig'): + Axes3D.set_zlabel_orig = Axes3D.set_zlabel + Axes3D.set_zlabel = set_zlabel -# initialization: -if xkcd_style: - plt.xkcd() + +def sketch_style(): + """ Activate xkcd style and adapt some rc parameter. + """ + global bar_fac bar_fac = 0.9 + plt.xkcd() + common_format() + mpl.rcParams['legend.fontsize'] = 'medium' + mpl.rcParams['xtick.labelsize'] = 'medium' + mpl.rcParams['ytick.labelsize'] = 'medium' mpl.rcParams['xtick.major.size'] = 6 mpl.rcParams['ytick.major.size'] = 6 -else: + + +def plain_style(): + """ Deactivate xkcd style and adapt some rc parameter. + """ + global bar_fac bar_fac = 1.0 + plt.rcdefaults() + common_format() mpl.rcParams['font.family'] = 'sans-serif' + mpl.rcParams['legend.fontsize'] = 'x-small' mpl.rcParams['xtick.labelsize'] = 'small' mpl.rcParams['ytick.labelsize'] = 'small' mpl.rcParams['xtick.major.size'] = 2.5 mpl.rcParams['ytick.major.size'] = 2.5 - mpl.rcParams['legend.fontsize'] = 'x-small' -mpl.rcParams['figure.figsize'] = cm_size(figure_width, figure_height) -mpl.rcParams['figure.subplot.left'] = 5.5*0.6*mpl.rcParams['font.size']/cm_size(figure_width)/ppi -mpl.rcParams['figure.subplot.right'] = 1.0 - 0.5*0.6*mpl.rcParams['font.size']/cm_size(figure_width)/ppi -mpl.rcParams['figure.subplot.bottom'] = 2.8*mpl.rcParams['font.size']/cm_size(figure_height)/ppi -mpl.rcParams['figure.subplot.top'] = 1.0 - 0.5*mpl.rcParams['font.size']/cm_size(figure_height)/ppi -mpl.rcParams['figure.subplot.wspace'] = 0.4 -mpl.rcParams['figure.subplot.hspace'] = 0.6 -mpl.rcParams['figure.facecolor'] = 'white' -#mpl.rcParams['axes.spines.left'] = True # newer matplotlib only -#mpl.rcParams['axes.spines.bottom'] = True -#mpl.rcParams['axes.spines.top'] = False -#mpl.rcParams['axes.spines.right'] = False -mpl.rcParams['xtick.direction'] = 'out' -mpl.rcParams['ytick.direction'] = 'out' -mpl.rcParams['xtick.major.width'] = 1.25 -mpl.rcParams['ytick.major.width'] = 1.25 + +def plot_style(): + """ Set rc parameter in dependence on xkcd_style. + """ + if xkcd_style: + sketch_style() + else: + plain_style() + + +# automatic initialization: +plot_style() diff --git a/regression/lecture/derivative.py b/regression/lecture/derivative.py index e5d4ea0..a377109 100644 --- a/regression/lecture/derivative.py +++ b/regression/lecture/derivative.py @@ -2,6 +2,8 @@ import numpy as np import matplotlib.pyplot as plt from plotstyle import * +plain_style() + fig = plt.figure( figsize=(2.5,3.4) ) ax = fig.add_subplot(1, 1, 1) diff --git a/regression/lecture/gradient.py b/regression/lecture/gradient.py index a64687f..a191eeb 100644 --- a/regression/lecture/gradient.py +++ b/regression/lecture/gradient.py @@ -2,6 +2,8 @@ import numpy as np import matplotlib.pyplot as plt from plotstyle import * +plain_style() + fig, ax = plt.subplots( figsize=(3.5,3.7) ) fig.subplots_adjust(**adjust_fs(fig, 4.0, 1.0, 2.8, 0.5))