Skip to content

Commit 864de9d

Browse files
committed
Refactor plotting functions for improved clarity and functionality
- Simplified the plot builder selection logic in the summary_plot function by using a conditional expression. - Enhanced the cycles_plot function to include a new optional parameter, return_data, allowing users to retrieve the data used for the plot. - Updated the return documentation for cycles_plot to clarify the output based on the new return_data parameter. - Adjusted the handling of return values in both plotting functions to ensure consistent behavior.
1 parent c386658 commit 864de9d

1 file changed

Lines changed: 33 additions & 29 deletions

File tree

cellpy/utils/plotutils.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,25 +4604,15 @@ def summary_plot(
46044604
plot_info,
46054605
)
46064606

4607-
# Build plot using new builder classes
4608-
if config.interactive:
4609-
builder = PlotlyPlotBuilder()
4610-
fig = builder.build_plot(
4611-
prepared_data_info["data"],
4612-
prepared_data_info,
4613-
config,
4614-
config.additional_kwargs,
4615-
c,
4616-
)
4617-
else:
4618-
builder = SeabornPlotBuilder()
4619-
fig = builder.build_plot(
4620-
prepared_data_info["data"],
4621-
prepared_data_info,
4622-
config,
4623-
config.additional_kwargs,
4624-
c,
4625-
)
4607+
builder = PlotlyPlotBuilder() if config.interactive else SeabornPlotBuilder()
4608+
4609+
fig = builder.build_plot(
4610+
prepared_data_info["data"],
4611+
prepared_data_info,
4612+
config,
4613+
config.additional_kwargs,
4614+
c,
4615+
)
46264616

46274617
if config.return_data:
46284618
return fig, prepared_data_info["data"]
@@ -5412,6 +5402,7 @@ def cycles_plot(
54125402
plotly_template=None,
54135403
seaborn_palette: str = "deep",
54145404
seaborn_style: str = "dark",
5405+
return_data=False,
54155406
**kwargs,
54165407
):
54175408
"""
@@ -5450,6 +5441,7 @@ def cycles_plot(
54505441
plotly_template (str, optional): Plotly template to use (uses default template if None).
54515442
seaborn_palette: name of the seaborn palette to use (only if seaborn is available).
54525443
seaborn_style: name of the seaborn style to use (only if seaborn is available).
5444+
return_data (bool, optional): Whether to return the data used for the plot. Default is False.
54535445
**kwargs: Additional keyword arguments for the plotting backend.
54545446
54555447
Additional keyword arguments for Plotly:
@@ -5459,7 +5451,13 @@ def cycles_plot(
54595451
plotly_layout_kwargs (dict, optional): propagated to plotly.update_layout.
54605452
54615453
Returns:
5462-
matplotlib.figure.Figure or plotly.graph_objects.Figure: The generated plot figure.
5454+
The figure is a matplotlib.figure.Figure or a plotly.graph_objects.Figure, depending on the backend used.
5455+
If return_data is True:
5456+
tuple: (figure, data)
5457+
If return_figure is True:
5458+
figure: The generated plot figure (same as the return value).
5459+
Else:
5460+
None: The plot is shown in the default browser.
54635461
"""
54645462

54655463
if interactive and not plotly_available:
@@ -5555,15 +5553,20 @@ def cycles_plot(
55555553

55565554
if interactive:
55575555
fig = _cycles_plotter_plotly(c, df, config, **kwargs)
5558-
if return_figure:
5556+
if return_data:
5557+
return fig, df
5558+
elif return_figure:
55595559
return fig
5560-
fig.show()
5560+
else:
5561+
fig.show()
55615562

55625563
else:
55635564
fig = _cycles_plotter_matplotlib(c, df, config, **kwargs)
5564-
print(type(fig))
5565-
if return_figure:
5565+
if return_figure or return_data:
55665566
plt.close(fig)
5567+
if return_data:
5568+
return fig, df
5569+
elif return_figure:
55675570
return fig
55685571

55695572

@@ -5921,10 +5924,11 @@ def _check_summary_plotter_seaborn():
59215924
fig = summary_plot(
59225925
c,
59235926
# x="normalized_cycle_index",
5924-
y="capacities_gravimetric_split_constant_voltage",
5925-
# fullcell_standard_normalization_type="on-cycles",
5927+
# y="capacities_gravimetric_split_constant_voltage",
5928+
y="fullcell_standard_gravimetric",
5929+
fullcell_standard_normalization_type="on-cycles",
59265930
# fullcell_standard_normalization_factor=1500.0,
5927-
# fullcell_standard_normalization_cycle_numbers=[18],
5931+
fullcell_standard_normalization_cycle_numbers=[18],
59285932
# ce_range=[0.0, 200.0],
59295933
# ylim=[0.0, 1.0],
59305934
# show_formation=False,
@@ -5978,6 +5982,6 @@ def _check_cycles_plotter_matplotlib():
59785982
# _check_plotter_plotly()
59795983
# _check_plotter_matplotlib()
59805984
# _check_summary_plotter_plotly()
5981-
# _check_summary_plotter_seaborn()
5982-
_check_cycles_plotter_plotly()
5985+
_check_summary_plotter_seaborn()
5986+
# _check_cycles_plotter_plotly()
59835987
# _check_cycles_plotter_matplotlib()

0 commit comments

Comments
 (0)