optional xkcd plot style

This commit is contained in:
Jan Benda 2019-12-26 18:49:19 +01:00
parent b5607deecb
commit 6604261978
3 changed files with 46 additions and 16 deletions

View File

@ -1,6 +1,8 @@
import matplotlib as mpl
import matplotlib.pyplot as plt
xkcd_style = False
# colors:
colors = {
'red': '#CC0000',
@ -98,9 +100,39 @@ def show_spines(ax, spines):
else:
ax.yaxis.set_ticks_position('both')
def set_xlabel(ax, label, unit=None):
if not unit:
ax.set_xlabel(label)
elif xkcd_style:
ax.set_xlabel('%s / %s' % (label, unit))
else:
ax.set_xlabel('%s [%s]' % (label, unit))
def set_ylabel(ax, label, unit=None):
if not unit:
ax.set_ylabel(label)
elif xkcd_style:
ax.set_ylabel('%s / %s' % (label, unit))
else:
ax.set_ylabel('%s [%s]' % (label, unit))
# initialization:
plt.xkcd()
if xkcd_style:
plt.xkcd()
bar_fac = 0.9
mpl.rcParams['xtick.major.size'] = 6
mpl.rcParams['ytick.major.size'] = 6
else:
bar_fac = 1.0
mpl.rcParams['font.family'] = 'sans-serif'
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.facecolor'] = 'white'
#mpl.rcParams['axes.spines.left'] = True # newer matplotlib only
#mpl.rcParams['axes.spines.bottom'] = True
@ -108,8 +140,6 @@ mpl.rcParams['figure.facecolor'] = 'white'
#mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['xtick.direction'] = 'out'
mpl.rcParams['ytick.direction'] = 'out'
mpl.rcParams['xtick.major.size'] = 6
mpl.rcParams['ytick.major.size'] = 6
mpl.rcParams['xtick.major.width'] = 1.25
mpl.rcParams['ytick.major.width'] = 1.25

View File

@ -2,7 +2,7 @@ import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import colors, cm_size, show_spines
from plotstyle import colors, cm_size, show_spines, set_xlabel, set_ylabel, bar_fac
if __name__ == "__main__":
# wikipedia:
@ -21,8 +21,8 @@ if __name__ == "__main__":
ax1 = fig.add_subplot(spec[0, 0])
show_spines(ax1, 'lb')
ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50)
ax1.set_xlabel('index')
ax1.set_ylabel('Weight / kg')
set_xlabel(ax1, 'index')
set_ylabel(ax1, 'Weight', 'kg')
ax1.set_xlim(-10, 310)
ax1.set_ylim(0, 370)
ax1.set_yticks(np.arange(0, 351, 100))
@ -31,11 +31,11 @@ if __name__ == "__main__":
show_spines(ax2, 'lb')
xx = np.arange(0.0, 350.0, 0.5)
yy = st.norm.pdf(xx, mu, sigma)
ax2.plot(yy, xx, color=colors['red'])
ax2.plot(yy, xx, color=colors['red'], lw=2)
bw = 20.0
h, b = np.histogram(data, np.arange(0, 401, bw))
ax2.barh(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], height=0.9*bw, align='edge')
ax2.set_xlabel('pdf / 1/kg')
ax2.barh(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], height=bar_fac*bw, align='edge')
set_xlabel(ax2, 'pdf', '1/kg')
ax2.set_xlim(0, 0.012)
ax2.set_xticks([0, 0.005, 0.01])
ax2.set_xticklabels(['0', '0.005', '0.01'])

View File

@ -2,7 +2,7 @@ import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import colors, cm_size, show_spines
from plotstyle import colors, cm_size, show_spines, set_xlabel, set_ylabel, bar_fac
def boltzmann(x, x0, k):
return 1.0/(1.0+np.exp(-k*(x-x0)))
@ -27,8 +27,8 @@ if __name__ == "__main__":
show_spines(ax1, 'lb')
ax1.plot(xx, yy, colors['red'], lw=2)
ax1.scatter(x, y, c=colors['blue'], edgecolor='white', s=50)
ax1.set_xlabel('Hair deflection / nm')
ax1.set_ylabel('Open probability')
set_xlabel(ax1, 'Hair deflection', 'nm')
set_ylabel(ax1, 'Open probability')
ax1.set_xlim(-20, 20)
ax1.set_ylim(-0.2, 1.17)
ax1.set_xticks(np.arange(-20.0, 21.0, 10.0))
@ -41,11 +41,11 @@ if __name__ == "__main__":
ax2.plot(xg, yg, colors['red'], lw=2)
bw = 0.05
h, b = np.histogram(y-boltzmann(x, x0, k), np.arange(-1.0, 1.01, bw))
ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], width=0.9*bw, align='edge')
ax2.set_xlabel('residuals')
ax2.set_ylabel('pdf')
ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], width=bar_fac*bw, align='edge')
set_xlabel(ax2, 'residuals', 'nm')
set_ylabel(ax2, 'pdf')
ax2.set_xlim(-0.3, 0.3)
#ax2.set_ylim(0, 370)
ax2.set_ylim(0, 5.05)
#ax2.set_xticks([0, 0.005, 0.01])
#ax2.set_xticklabels(['0', '0.005', '0.01'])
#ax2.set_yticks(np.arange(0, 351, 100))