5151from typing import Callable
5252from spotpython .utils .numpy2json import NumpyEncoder
5353from 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 ,
0 commit comments