Skip to content

Commit 22a9a0c

Browse files
authored
Allow specifying file format for visualization (#85)
So far, it was only possible to generate PNGs. This is still the default, but can be changed to any other format supported by matplotlib
1 parent 918e1a4 commit 22a9a0c

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

petab/visualize/plot_data_and_simulation.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ def plot_with_vis_spec(
372372
measurements_df: Optional[Union[str, pd.DataFrame]] = None,
373373
simulations_df: Optional[Union[str, pd.DataFrame]] = None,
374374
subplot_dir: Optional[str] = None,
375-
plotter_type: str = 'mpl') -> Optional[Dict[str, plt.Subplot]]:
375+
plotter_type: str = 'mpl',
376+
format_: str = 'png',
377+
) -> Optional[Dict[str, plt.Subplot]]:
376378
"""
377379
Plot measurements and/or simulations. Specification of the visualization
378380
routines is provided in visualization table.
@@ -394,6 +396,9 @@ def plot_with_vis_spec(
394396
plotter_type:
395397
Specifies which library should be used for plot generation. Currently,
396398
only matplotlib is supported.
399+
format_:
400+
File format for the generated figure.
401+
(See :py:func:`matplotlib.pyplot.savefig` for supported options).
397402
398403
Returns
399404
-------
@@ -415,8 +420,8 @@ def plot_with_vis_spec(
415420
else:
416421
raise NotImplementedError('Currently, only visualization with '
417422
'matplotlib is possible.')
418-
ax = plotter.generate_figure(subplot_dir)
419-
return ax
423+
424+
return plotter.generate_figure(subplot_dir, format_=format_)
420425

421426

422427
def plot_without_vis_spec(
@@ -427,7 +432,8 @@ def plot_without_vis_spec(
427432
simulations_df: Optional[Union[str, pd.DataFrame]] = None,
428433
plotted_noise: str = MEAN_AND_SD,
429434
subplot_dir: Optional[str] = None,
430-
plotter_type: str = 'mpl'
435+
plotter_type: str = 'mpl',
436+
format_: str = 'png',
431437
) -> Optional[Dict[str, plt.Subplot]]:
432438
"""
433439
Plot measurements and/or simulations. What exactly should be plotted is
@@ -462,6 +468,9 @@ def plot_without_vis_spec(
462468
plotter_type:
463469
Specifies which library should be used for plot generation. Currently,
464470
only matplotlib is supported
471+
format_:
472+
File format for the generated figure.
473+
(See :py:func:`matplotlib.pyplot.savefig` for supported options).
465474
466475
Returns
467476
-------
@@ -484,8 +493,8 @@ def plot_without_vis_spec(
484493
else:
485494
raise NotImplementedError('Currently, only visualization with '
486495
'matplotlib is possible.')
487-
ax = plotter.generate_figure(subplot_dir)
488-
return ax
496+
497+
return plotter.generate_figure(subplot_dir, format_=format_)
489498

490499

491500
def plot_problem(petab_problem: problem.Problem,

petab/visualize/plotter.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,11 @@ def ticks(y, _):
321321
ax.set_ylabel(
322322
subplot.yLabel)
323323

324-
def generate_figure(self, subplot_dir: Optional[str] = None
325-
) -> Optional[Dict[str, plt.Subplot]]:
324+
def generate_figure(
325+
self,
326+
subplot_dir: Optional[str] = None,
327+
format_: str = 'png',
328+
) -> Optional[Dict[str, plt.Subplot]]:
326329
"""
327330
Generate the full figure based on the markup in the figure attribute.
328331
@@ -331,6 +334,9 @@ def generate_figure(self, subplot_dir: Optional[str] = None
331334
subplot_dir:
332335
A path to the folder where single subplots should be saved.
333336
PlotIDs will be taken as file names.
337+
format_:
338+
File format for the generated figure.
339+
(See :py:func:`matplotlib.pyplot.savefig` for supported options).
334340
335341
Returns
336342
-------
@@ -378,7 +384,7 @@ def generate_figure(self, subplot_dir: Optional[str] = None
378384
# TODO: why this doesn't work?
379385
plt.tight_layout()
380386
plt.savefig(os.path.join(subplot_dir,
381-
f'{subplot.plotId}.png'))
387+
f'{subplot.plotId}.{format_}'))
382388
plt.close()
383389

384390
if subplot_dir is None:

0 commit comments

Comments
 (0)