Skip to content

Commit c3c1951

Browse files
0.31.2
plot_3d_contour is a separate function
1 parent d809f44 commit c3c1951

4 files changed

Lines changed: 76 additions & 106 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 2 additions & 22 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.31.1"
10+
version = "0.31.2"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/spot/spot.py

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from typing import Callable
5252
from spotpython.utils.numpy2json import NumpyEncoder
5353
from spotpython.utils.file import load_result
54+
from spotpython.surrogate.plot import plot_3d_contour
5455

5556
# Setting up the backend to use QtAgg
5657
# matplotlib.use("TkAgg")
@@ -187,7 +188,7 @@ def objective_function(X, fun_control=None):
187188
optimizer_control=optimizer_control)
188189
spot.run()
189190
spot.plot_progress()
190-
spot.prepare_plot_contour(i=0, j=1)
191+
spot.plot_contour(i=0, j=1)
191192
spot.plot_importance()
192193
"""
193194

@@ -2431,8 +2432,7 @@ def process_z00(self, z00, use_min=True) -> list:
24312432
result.append(max_value)
24322433
return result
24332434

2434-
2435-
def prepare_plot_contour(
2435+
def plot_contour(
24362436
self,
24372437
i=0,
24382438
j=1,
@@ -2482,7 +2482,7 @@ def prepare_plot_contour(
24822482
use_min=use_min,
24832483
use_max=use_max,
24842484
)
2485-
self.plot_contour(
2485+
plot_3d_contour(
24862486
plot_data,
24872487
i=i,
24882488
j=j,
@@ -2578,83 +2578,6 @@ def predict_contour_values(X, Y, z0):
25782578
"max_z": max_z,
25792579
}
25802580

2581-
def plot_contour(
2582-
self,
2583-
plot_data: dict,
2584-
i=0,
2585-
j=1,
2586-
show=True,
2587-
filename=None,
2588-
contour_levels=10,
2589-
dpi=200,
2590-
title=None,
2591-
figsize=(12, 6),
2592-
tkagg=False,
2593-
) -> None:
2594-
"""
2595-
Plot the contour and 3D surface using prepared data.
2596-
2597-
Args:
2598-
plot_data (dict): Output from prepare_plot().
2599-
i (int, optional): Index of the first dimension to plot. Default is 0.
2600-
j (int, optional): Index of the second dimension to plot. Default is 1.
2601-
show (bool, optional): Whether to display the plot interactively. Default is True.
2602-
filename (str, optional): If provided, saves the plot to this file. Default is None.
2603-
contour_levels (int, optional): Number of contour levels. Default is 10.
2604-
dpi (int, optional): Dots per inch for saved figure. Default is 200.
2605-
title (str, optional): Title for the plot. Default is None.
2606-
figsize (tuple, optional): Figure size in inches (width, height). Default is (12, 6).
2607-
tkagg (bool, optional): If True, use TkAgg backend for matplotlib. Default is False.
2608-
2609-
Returns:
2610-
None
2611-
2612-
Examples:
2613-
>>> plot_data = S.prepare_plot(i=0, j=1)
2614-
>>> S.plot_contour(plot_data, i=0, j=1, title="Surrogate Contour Plot")
2615-
"""
2616-
X_combined = plot_data["X_combined"]
2617-
Y_combined = plot_data["Y_combined"]
2618-
Z_combined = plot_data["Z_combined"]
2619-
min_z = plot_data["min_z"]
2620-
max_z = plot_data["max_z"]
2621-
2622-
if tkagg:
2623-
matplotlib.use("TkAgg")
2624-
fig = pylab.figure(figsize=figsize)
2625-
2626-
ax_3d = fig.add_subplot(121, projection="3d")
2627-
ax_3d.plot_surface(X_combined, Y_combined, Z_combined, rstride=3, cstride=3, alpha=0.9, cmap="jet", vmin=min_z, vmax=max_z)
2628-
2629-
if self.var_name is None:
2630-
ax_3d.set_xlabel(f"x{i}")
2631-
ax_3d.set_ylabel(f"x{j}")
2632-
else:
2633-
ax_3d.set_xlabel(f"x{i}: {self.var_name[i]}")
2634-
ax_3d.set_ylabel(f"x{j}: {self.var_name[j]}")
2635-
2636-
ax_contour = fig.add_subplot(122)
2637-
if title is not None:
2638-
ax_3d.set_title(title)
2639-
2640-
contour = ax_contour.contourf(X_combined, Y_combined, Z_combined, levels=contour_levels, zorder=1, cmap="jet", vmin=min_z, vmax=max_z)
2641-
pylab.colorbar(contour, ax=ax_contour)
2642-
2643-
if self.var_name is None:
2644-
ax_contour.set_xlabel(f"x{i}")
2645-
ax_contour.set_ylabel(f"x{j}")
2646-
else:
2647-
ax_contour.set_xlabel(f"x{i}: {self.var_name[i]}")
2648-
ax_contour.set_ylabel(f"x{j}: {self.var_name[j]}")
2649-
2650-
if title is not None:
2651-
ax_contour.set_title(title)
2652-
if filename:
2653-
pylab.savefig(filename, bbox_inches="tight", dpi=dpi, pad_inches=0)
2654-
2655-
if show:
2656-
pylab.show()
2657-
26582581
def plot_important_hyperparameter_contour(
26592582
self,
26602583
threshold=0.0,
@@ -2672,7 +2595,7 @@ def plot_important_hyperparameter_contour(
26722595
) -> None:
26732596
"""
26742597
Plot the contour of important hyperparameters.
2675-
Calls `prepare_plot_contour` for each pair of important hyperparameters.
2598+
Calls `plot_contour` for each pair of important hyperparameters.
26762599
Importance can be specified by the threshold.
26772600
26782601
Args:
@@ -2759,7 +2682,7 @@ def plot_important_hyperparameter_contour(
27592682
filename_full = filename + "_contour_" + str(i) + "_" + str(j) + ".png"
27602683
else:
27612684
filename_full = None
2762-
self.prepare_plot_contour(
2685+
self.plot_contour(
27632686
i=i,
27642687
j=j,
27652688
min_z=min_z,

src/spotpython/surrogate/plot.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,70 @@ def plotkd(model, X: np.ndarray, y: np.ndarray, i: int, j: int, show: Optional[b
292292

293293
if show:
294294
plt.show()
295+
296+
297+
def plot_3d_contour(
298+
plot_data: dict,
299+
var_name=None,
300+
i=0,
301+
j=1,
302+
show=True,
303+
filename=None,
304+
contour_levels=10,
305+
dpi=200,
306+
title=None,
307+
figsize=(12, 6),
308+
tkagg=False,
309+
) -> None:
310+
"""
311+
Plot the contour and 3D surface using prepared data.
312+
313+
Args:
314+
plot_data (dict): Output from prepare_plot().
315+
var_name (list or None): List of variable names, or None.
316+
i (int, optional): Index of the first dimension to plot. Default is 0.
317+
j (int, optional): Index of the second dimension to plot. Default is 1.
318+
show (bool, optional): Whether to display the plot interactively. Default is True.
319+
filename (str, optional): If provided, saves the plot to this file. Default is None.
320+
contour_levels (int, optional): Number of contour levels. Default is 10.
321+
dpi (int, optional): Dots per inch for saved figure. Default is 200.
322+
title (str, optional): Title for the plot. Default is None.
323+
figsize (tuple, optional): Figure size in inches (width, height). Default is (12, 6).
324+
tkagg (bool, optional): If True, use TkAgg backend for matplotlib. Default is False.
325+
326+
Returns:
327+
None
328+
329+
Examples:
330+
>>> plot_data = S.prepare_plot(i=0, j=1)
331+
>>> plot__3d_contour(plot_data, var_name=S.var_name, i=0, j=1, title="Surrogate Contour Plot")
332+
"""
333+
import matplotlib
334+
import pylab
335+
336+
X_combined = plot_data["X_combined"]
337+
Y_combined = plot_data["Y_combined"]
338+
Z_combined = plot_data["Z_combined"]
339+
min_z = plot_data["min_z"]
340+
max_z = plot_data["max_z"]
341+
342+
if tkagg:
343+
matplotlib.use("TkAgg")
344+
fig = pylab.figure(figsize=figsize)
345+
346+
ax_3d = fig.add_subplot(121, projection="3d")
347+
ax_3d.plot_surface(X_combined, Y_combined, Z_combined, rstride=3, cstride=3, alpha=0.9, cmap="jet", vmin=min_z, vmax=max_z)
348+
349+
if var_name is None:
350+
ax_3d.set_xlabel(f"x{i}")
351+
ax_3d.set_ylabel(f"x{j}")
352+
else:
353+
ax_3d.set_xlabel(f"x{i}: {var_name[i]}")
354+
ax_3d.set_ylabel(f"x{j}: {var_name[j]}")
355+
356+
ax_contour = fig.add_subplot(122)
357+
if title is not None:
358+
ax_3d.set_title(title)
359+
360+
contour = ax_contour.contourf(X_combined, Y_combined, Z_combined, levels=contour_levels, zorder=1, cmap="jet", vmin=min_z, vmax=max_z)
361+
pylab.colorbar(contour, ax=ax_contour)

0 commit comments

Comments
 (0)