bachelor_thesis/prog/python/utility.py

75 lines
2.2 KiB
Python
Raw Normal View History

2020-03-26 21:45:19 +01:00
import matplotlib
import matplotlib.pyplot as plt
from SecondaryValue import SecondaryValue
from scipy.constants import hbar, c, electron_volt
2020-03-27 19:34:22 +01:00
import numpy as np
2020-03-26 21:45:19 +01:00
###############################################################################
# Utility #
###############################################################################
def gev_to_pb(xs):
"""Converts a cross section from 1/GeV^2 to pb."""
return xs/(electron_volt**2)*(hbar*c)**2*1e22
2020-03-27 19:34:22 +01:00
def tex_value(val, unit='', prefix='', prec=10, err=None):
"""Generates LaTeX output of a value with units and error."""
val = np.round(val, prec)
return fr'\({prefix}\SI{{{val}}}{{{unit}}}\)'
2020-03-26 21:45:19 +01:00
###############################################################################
# Plot Porn #
###############################################################################
matplotlib.rcParams.update({
'font.family': 'serif',
'text.usetex': False,
'pgf.rcfonts': False,
})
def pinmp_ticks(axis, ticks):
axis.set_major_locator(ticker.MaxNLocator(ticks))
axis.set_minor_locator(ticker.MaxNLocator(ticks*10))
return axis
def set_up_plot(ticks=10, pimp_top=True, subplot=111, fig=None):
if fig is None:
fig = plt.figure()
ax = fig.add_subplot(subplot)
pinmp_ticks(ax.xaxis, ticks)
pinmp_ticks(ax.yaxis, ticks)
ax.grid(which='minor', alpha=.3)
ax.grid(which='major', alpha=.5)
if pimp_top:
ax.tick_params(right=True, top=True, which='both')
else:
ax.tick_params(right=True, which='both')
return fig, ax
def save_fig(fig, title, folder='unsorted', size=(5, 4)):
fig.set_size_inches(*size)
fig.tight_layout()
try:
os.makedirs(f'./figs/{folder}/')
except OSError as exc:
pass
fig.savefig(f'./figs/{folder}/{title}.pdf')
fig.savefig(f'./figs/{folder}/{title}.pgf')
with open('./out/figlist.txt', 'a') as f:
f.write(r'''
\begin{figure}[H]\centering
\input{../auswertung/figs/'''
+ f'{folder}/{title}.pgf' +
r'''}
\caption{}
\label{fig:''' + folder + '-' + title + r'''}
\end{figure}
''')