-
Notifications
You must be signed in to change notification settings - Fork 46
Implementation of matplotlib
backend for criterion_plot()
#599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e187294
Implement matplotlib backend for criterion plot
r3kste 0159fe6
Refactor plotting backend structure and remove PlotConfig class.
r3kste e167b49
Make matplotlib an optional dependency and minor refactor for clarity.
r3kste f15d982
Enhance availability check for backends. Fix issues with matplotlib i…
r3kste f1fcdaa
Refactor to functional approach for backend plotting. Use hardcoded d…
r3kste bdb5efe
Add testing file for backends. Refactor matplotlib backend to return …
r3kste 8324199
Merge branch 'main' into backend_plotting
timmens 2b94df7
Refactor matplotlib backend to use a context manager for template.
r3kste 141eba7
Merge branch 'main' into backend_plotting
r3kste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r3kste marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import abc | ||
from typing import Any | ||
|
||
import matplotlib as mpl | ||
import matplotlib.pyplot as plt | ||
import plotly.express as px | ||
import plotly.graph_objects as go | ||
|
||
from optimagic.visualization.plotting_utilities import LineData | ||
|
||
|
||
class PlotBackend(abc.ABC): | ||
default_template: str | ||
default_palette: list | ||
|
||
@abc.abstractmethod | ||
def __init__(self, template: str | None): | ||
if template is None: | ||
template = self.default_template | ||
|
||
self.template = template | ||
self.figure: Any = None | ||
|
||
@abc.abstractmethod | ||
def add_lines(self, lines: list[LineData]) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def set_labels(self, xlabel: str | None = None, ylabel: str | None = None) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def set_legend_props(self, legend_props: dict[str, Any]) -> None: | ||
pass | ||
|
||
|
||
class PlotlyBackend(PlotBackend): | ||
default_template: str = "simple_white" | ||
default_palette: list = px.colors.qualitative.Set2 | ||
|
||
def __init__(self, template: str | None): | ||
super().__init__(template) | ||
self._fig = go.Figure() | ||
self._fig.update_layout(template=self.template) | ||
self.figure = self._fig | ||
|
||
def add_lines(self, lines: list[LineData]) -> None: | ||
for line in lines: | ||
trace = go.Scatter( | ||
x=line.x, | ||
y=line.y, | ||
name=line.name, | ||
mode="lines", | ||
line_color=line.color, | ||
showlegend=line.show_in_legend, | ||
connectgaps=True, | ||
) | ||
self._fig.add_trace(trace) | ||
|
||
def set_labels(self, xlabel: str | None = None, ylabel: str | None = None) -> None: | ||
self._fig.update_layout(xaxis_title_text=xlabel, yaxis_title_text=ylabel) | ||
|
||
def set_legend_props(self, legend_props: dict[str, Any]) -> None: | ||
r3kste marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
self._fig.update_layout(legend=legend_props) | ||
|
||
|
||
class MatplotlibBackend(PlotBackend): | ||
default_template: str = "default" | ||
default_palette: list = list(mpl.colormaps["Set2"].colors) | ||
r3kste marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
def __init__(self, template: str | None): | ||
super().__init__(template) | ||
plt.style.use(self.template) | ||
self._fig, self._ax = plt.subplots() | ||
self.figure = self._fig | ||
|
||
def add_lines(self, lines: list[LineData]) -> None: | ||
for line in lines: | ||
self._ax.plot( | ||
line.x, | ||
line.y, | ||
color=line.color, | ||
label=line.name if line.show_in_legend else None, | ||
) | ||
|
||
def set_labels(self, xlabel: str | None = None, ylabel: str | None = None) -> None: | ||
self._ax.set(xlabel=xlabel, ylabel=ylabel) | ||
|
||
def set_legend_props(self, legend_props: dict[str, Any]) -> None: | ||
self._ax.legend(**legend_props) | ||
|
||
|
||
PLOT_BACKEND_CLASSES = { | ||
"plotly": PlotlyBackend, | ||
"matplotlib": MatplotlibBackend, | ||
} | ||
|
||
|
||
def get_plot_backend_class(backend_name: str) -> type[PlotBackend]: | ||
if backend_name not in PLOT_BACKEND_CLASSES: | ||
msg = ( | ||
f"Invalid backend name '{backend_name}'. " | ||
f"Supported backends are: {', '.join(PLOT_BACKEND_CLASSES.keys())}." | ||
) | ||
raise ValueError(msg) | ||
|
||
return PLOT_BACKEND_CLASSES[backend_name] |
r3kste marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.