[plotstyle] adjust_fs() function for plot margins as multiples of font size
This commit is contained in:
parent
048bce0a20
commit
78cd41001a
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,6 +18,7 @@
|
||||
*.pdf
|
||||
*.eps
|
||||
*-solutions.tex
|
||||
*.pyc
|
||||
.#*
|
||||
#*#
|
||||
*~
|
||||
|
58
plotstyle.py
58
plotstyle.py
@ -1,12 +1,15 @@
|
||||
import matplotlib as mpl
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
xkcd_style = True
|
||||
xkcd_style = False
|
||||
|
||||
# default size of figure:
|
||||
figure_width = 15.0 # cm, should be set according to \textwidth in the latex document
|
||||
figure_height = 6.0 # cm, for a 1 x 2 figure
|
||||
|
||||
# points per inch:
|
||||
ppi = 72.0
|
||||
|
||||
# colors:
|
||||
colors = {
|
||||
'red': '#CC0000',
|
||||
@ -14,7 +17,7 @@ colors = {
|
||||
'lightorange': '#FFCC00',
|
||||
'yellow': '#FFFF66',
|
||||
'green': '#99FF00',
|
||||
'blue': '#0000FF'
|
||||
'blue': '#0000CC'
|
||||
}
|
||||
|
||||
|
||||
@ -43,6 +46,47 @@ def cm_size(*args):
|
||||
return [v/inch_per_cm for v in args]
|
||||
|
||||
|
||||
def adjust_fs(fig=None, left=5.5, right=0.5, bottom=2.8, top=0.5):
|
||||
""" Compute plot margins from multiples of the current font size.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
fig: matplotlib.figure or None
|
||||
The figure from which the figure size is taken. If None use the current figure.
|
||||
left: float
|
||||
the left margin of the plots given in multiples of the width of a character
|
||||
(in fact, simply 60% of the current font size).
|
||||
right: float
|
||||
the right margin of the plots given in multiples of the width of a character
|
||||
(in fact, simply 60% of the current font size).
|
||||
*Note:* in contrast to the matplotlib `right` parameters, this specifies the
|
||||
width of the right margin, not its position relative to the origin.
|
||||
bottom: float
|
||||
the bottom margin of the plots given in multiples of the height of a character
|
||||
(the current font size).
|
||||
top: float
|
||||
the right margin of the plots given in multiples of the height of a character
|
||||
(the current font size).
|
||||
*Note:* in contrast to the matplotlib `top` parameters, this specifies the
|
||||
width of the top margin, not its position relative to the origin.
|
||||
|
||||
Example
|
||||
-------
|
||||
```
|
||||
fig, axs = plt.subplots(2, 2, figsize=(10, 5))
|
||||
fig.subplots_adjust(**adjust_fs(fig, left=4.5)) # no matter what the figsize is!
|
||||
```
|
||||
"""
|
||||
if fig is None:
|
||||
fig = plt.gcf()
|
||||
w, h = fig.get_size_inches()*ppi
|
||||
fs = plt.rcParams['font.size']
|
||||
return { 'left': left*0.6*fs/w,
|
||||
'right': 1.0 - right*0.6*fs/w,
|
||||
'bottom': bottom*fs/h,
|
||||
'top': 1.0 - top*fs/h }
|
||||
|
||||
|
||||
def show_spines(ax, spines):
|
||||
""" Show and hide spines.
|
||||
|
||||
@ -138,12 +182,12 @@ else:
|
||||
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'] = 0.11
|
||||
mpl.rcParams['figure.subplot.right'] = 0.99
|
||||
mpl.rcParams['figure.subplot.bottom'] = 0.23
|
||||
mpl.rcParams['figure.subplot.top'] = 0.96
|
||||
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.2
|
||||
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
|
||||
|
@ -16,7 +16,8 @@ if __name__ == "__main__":
|
||||
data = sigma*rng.randn(len(indices))+mu
|
||||
|
||||
fig = plt.figure()
|
||||
spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], left=0.12, wspace=0.08)
|
||||
spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], wspace=0.08,
|
||||
**adjust_fs(fig, left=6.0))
|
||||
ax1 = fig.add_subplot(spec[0, 0])
|
||||
show_spines(ax1, 'lb')
|
||||
ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50)
|
||||
|
@ -21,9 +21,7 @@ if __name__ == "__main__":
|
||||
corrs = [np.corrcoef(x[maxl:-maxl-1-l], x[maxl+l:-maxl-1])[0, 1] for l in lags]
|
||||
|
||||
fig = plt.figure(figsize=cm_size(figure_width, 1.8*figure_height))
|
||||
spec = gridspec.GridSpec(nrows=2, ncols=2,
|
||||
left=0.11, bottom=0.12, right=0.98, top=0.97,
|
||||
wspace=0.4, hspace=0.6)
|
||||
spec = gridspec.GridSpec(nrows=2, ncols=2, **adjust_fs(fig))
|
||||
|
||||
ax = fig.add_subplot(spec[0, 0])
|
||||
show_spines(ax, 'lb')
|
||||
|
@ -21,7 +21,7 @@ if __name__ == "__main__":
|
||||
yy = boltzmann(xx, x0, k)
|
||||
|
||||
fig = plt.figure()
|
||||
spec = gridspec.GridSpec(nrows=1, ncols=2, left=0.1, wspace=0.4)
|
||||
spec = gridspec.GridSpec(nrows=1, ncols=2, **adjust_fs(fig, left=4.5))
|
||||
ax1 = fig.add_subplot(spec[0, 0])
|
||||
show_spines(ax1, 'lb')
|
||||
ax1.plot(xx, yy, colors['red'], lw=2)
|
||||
|
Reference in New Issue
Block a user