use gnuplot as lightweight plotter

This commit is contained in:
Dominik Demuth 2023-05-19 11:33:02 +02:00
parent 233cdd9f80
commit 6e9dd4d45f

View File

@ -11,6 +11,7 @@ import numpy as np
from nmreval.data.points import Points from nmreval.data.points import Points
from nmreval.io.asciireader import AsciiReader from nmreval.io.asciireader import AsciiReader
from nmreval.io.hdfreader import HdfReader from nmreval.io.hdfreader import HdfReader
from nmreval.lib.logger import logger
from nmreval.utils.utils import get_temperature, roundrobin from nmreval.utils.utils import get_temperature, roundrobin
@ -56,7 +57,7 @@ class FCReader:
_temp = self._read_from_dir(filename) _temp = self._read_from_dir(filename)
else: else:
raise TypeError raise TypeError(f'{filename} is of unknown type')
if not _temp: if not _temp:
raise OSError(-666, f'No magnetization found for {filename.name}.', filename.name) raise OSError(-666, f'No magnetization found for {filename.name}.', filename.name)
@ -212,7 +213,7 @@ class FCReader:
except KeyError: except KeyError:
self.f_params[k] = [new_entry] self.f_params[k] = [new_entry]
if True: # save_fits or save_fig: if True: # save_fits or save_fig:
xplot = np.geomspace(v.x[0], v.x[-1], num=10*len(v.x)) xplot = np.geomspace(v.x[0], v.x[-1], num=10*len(v.x))
yplot = FCReader.kww(xplot, *p0) yplot = FCReader.kww(xplot, *p0)
save_name = f'{filename.stem}_{k:011.2f}'.replace('.', 'p') + '.dat' save_name = f'{filename.stem}_{k:011.2f}'.replace('.', 'p') + '.dat'
@ -224,33 +225,30 @@ class FCReader:
img_file = image_path.joinpath(save_name).with_suffix(".png") img_file = image_path.joinpath(save_name).with_suffix(".png")
gnuplot_args = [ gnuplot_args = [
'gnuplot', 'set terminal png;',
'-e', f'set output "{img_file}";',
'set terminal png', f'set title "f = {k:.4g} Hz\\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})";',
f'set output "{img_file}"', 'set xlabel "t / s";',
f'set title "f = {k:.4g} Hz\n{p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})"', 'set logscale x;',
'set xlabel "t / s"', 'set format x "10^{{%L}}";',
'set logscale x', 'set ylabel "M";',
'set format x "10^{{%L}}"', 'set key off;',
'set ylabel "M"', f'plot "{data_path.joinpath(save_name)}" with points pointtype 5, "{fit_path.joinpath(save_name)}" with lines;',
'set key off',
f'plot "{data_path.joinpath(save_name)}" with points pointtype 5, "{fit_path.joinpath(save_name)}" with lines',
] ]
print(gnuplot_args) try:
proc = subprocess.Popen(
subprocess.Popen(gnuplot_args) ['gnuplot', '-p'],
shell=True,
# if save_fig: stdin=subprocess.PIPE,
# fig, ax = plt.subplots() encoding='utf8',
# ax.set_xlabel('t / s') )
# ax.set_ylabel('M') for args in gnuplot_args:
# axheader = f'T1: {p0[2]:.4g}(+/-{perr[2]:.4g}) beta: {p0[3]:.4g}(+/-{perr[3]:.4g})' proc.stdin.write(args)
# ax.set_title(f'f = {k:.4g} Hz\n{axheader}') proc.stdin.write('quit\n')
# ax.semilogx(v.x, v.y, 'o') proc.stdin.flush()
# ax.semilogx(xplot, yplot, '-') except Exception as e:
# fig.savefig(image_path.joinpath(save_name).with_suffix('.png')) logger.error(f'saving image {save_name} failed', e)
# plt.close(fig)
freqs = np.asanyarray(freqs) freqs = np.asanyarray(freqs)
params = np.asanyarray(params) params = np.asanyarray(params)