feat: 🎨 adjustments to plots as requested by Moritz

This commit is contained in:
Xaseiresh 2024-05-03 16:35:12 +02:00
parent 14b40eb1e0
commit ccbea065c5
2 changed files with 46 additions and 22 deletions

View file

@ -40,7 +40,7 @@ def setup_step(plot_data, key):
plot_data[key] = next_step;
plot_data['steps'].append(next_step);
def read_ltspice_file(filename):
def read_ltspice_file(filename, plot_config):
print(f"Reading LTSpice .txt file {filename}...");
series = [];
@ -57,8 +57,13 @@ def read_ltspice_file(filename):
line = line.rstrip("\n");
lre = Re();
if lre.search("^Step Information: (.*) \(Step: .*\)$", line):
current_step = lre.last_match[1];
if lre.search("^Step Information: ([^=]*)=([\d\.\+-]+)(\w?) \(Step: .*\)$", line):
step_param = plot_config.get('step_parameter', lre.last_match[1]);
value = f"{round(float(lre.last_match[2]), 2)}";
unit = f"{lre.last_match[3]}{plot_config.get('step_unit', '')}";
current_step = "{value:>6s} {unit:s}".format(value=value, unit=unit);
else:
setup_step(plot_data, current_step);
step = plot_data[current_step];
@ -93,6 +98,11 @@ def decorate_ax(ax, plot_config):
if('xscale' in plot_config):
ax.set_xscale(plot_config['xscale']);
if('xmin' in plot_config):
ax.set_xlim(left=plot_config['xmin']);
if('xmax' in plot_config):
ax.set_xlim(right=plot_config['xmax']);
if('xformatter' in plot_config):
if('engineering' == plot_config['xformatter']):
formatter = EngFormatter(places=plot_config.get('xplaces', 0), sep="\N{THIN SPACE}")
@ -103,6 +113,14 @@ def decorate_ax(ax, plot_config):
formatter = EngFormatter(places=plot_config.get('yplaces', 0))
ax.yaxis.set_major_formatter(formatter)
legend = ax.legend();
hp = legend._legend_box.get_children()[1]
for vp in hp.get_children():
for row in vp.get_children():
row.set_width(350) # need to adapt this manually
row.mode= "expand"
row.align="right"
ax.grid(True);
def plot_lt_sweep(fig, plot_config, plot_data):
@ -130,20 +148,17 @@ def plot_lt_sweep(fig, plot_config, plot_data):
matplotlib.lines.Line2D([0], [0], color=cmap(1.), lw=4)];
for idx, step in enumerate(plot_data['steps']):
ax.plot(step[x_key], step[y_key], color=cmap(idx/(num_steps-1)));
ax.plot(step[x_key], step[y_key], color=cmap(idx/(num_steps-1)), label=step['step']);
if(not 'xformatter' in plot_config):
plot_config['xformatter'] = 'engineering';
legend_data = [];
for x in [0, int(num_steps/2), num_steps-1]:
step_name = plot_data['steps'][x]['step'];
for orig, replacement in plot_config.get('legend_replace', dict()).items():
step_name = step_name.replace(orig, replacement);
legend_data.append(step_name);
if(not 'xmin' in plot_config):
plot_config['xmin'] = np.min(plot_data['steps'][0][x_key]);
if(not 'xmax' in plot_config):
plot_config['xmax'] = np.max(plot_data['steps'][0][x_key]);
ax.legend(custom_lines, legend_data);
ax.legend();
decorate_ax(ax, plot_config);
@ -156,7 +171,7 @@ def generate_plot(plot_config):
raise RuntimeError("Missing load type (`loadtype`) for plot config");
if(plot_config['loadtype'] == 'ltspice'):
plot_data = read_ltspice_file(os.path.join(YAML_DIR, plot_config['load']));
plot_data = read_ltspice_file(os.path.join(YAML_DIR, plot_config['load']), plot_config);
fig = plt.figure();