plot.contour
plot.contour
Functions
| Name | Description |
|---|---|
| contourf_plot | Creates contour plots (single or faceted) using matplotlib. |
| mo_generate_plot_grid | Generate a grid of input variables and apply objective functions. |
| plotModel | Generate 2D contour and optionally 3D surface plots for a model’s predictions. |
| simple_contour | Simple contour plot |
contourf_plot
plot.contour.contourf_plot(
data,
x_col,
y_col,
z_col,
facet_col=None,
aspect=1,
as_table=True,
figsize=(4, 4),
levels=10,
cmap='viridis',
show_contour_lines=True,
contour_line_color='black',
contour_line_width=0.5,
colorbar_orientation='vertical',
wspace=0.4,
hspace=0.4,
highlight_point=None,
highlight_color='red',
highlight_marker='x',
highlight_size=100,
)Creates contour plots (single or faceted) using matplotlib.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | pd.DataFrame | Data for plotting. | required |
| x_col | str | Column name for x-axis. | required |
| y_col | str | Column name for y-axis. | required |
| z_col | str | Column name for z-axis (values). | required |
| facet_col | str | Column name for faceting. | None |
| aspect | float | Aspect ratio. | 1 |
| as_table | bool | If True, arranges facets in a table. | True |
| figsize | tuple | Figure size per plot key (if not faceted) or base. | (4, 4) |
| levels | int | Number of contour levels. | 10 |
| cmap | str | Colormap. | 'viridis' |
| show_contour_lines | bool | Whether to show contour lines. | True |
| contour_line_color | str | Color of contour lines. | 'black' |
| contour_line_width | float | Width of contour lines. | 0.5 |
| colorbar_orientation | str | ‘vertical’ or ‘horizontal’. | 'vertical' |
| wspace | float | Width reserved for space between subplots. | 0.4 |
| hspace | float | Height reserved for space between subplots. | 0.4 |
| highlight_point | dict - like |
Point to highlight. Must contain x_col, y_col keys. If faceted, must contain facet_col key. | None |
| highlight_color | str | Color of the highlight point. | 'red' |
| highlight_marker | str | Marker style for the highlight point. | 'x' |
| highlight_size | int | Size of the highlight point. | 100 |
mo_generate_plot_grid
plot.contour.mo_generate_plot_grid(variables, resolutions, functions)Generate a grid of input variables and apply objective functions.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| variables | dict | A dictionary where keys are variable names (e.g., “time”, “temperature”) and values are tuples of (min_value, max_value). | required |
| resolutions | dict | A dictionary where keys are variable names and values are the number of points. | required |
| functions | dict | A dictionary where keys are function names and values are callable functions. | required |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | pd.DataFrame: A DataFrame containing the grid and the results of the objective functions. |
plotModel
plot.contour.plotModel(
model,
lower,
upper,
i=0,
j=1,
min_z=None,
max_z=None,
var_type=None,
var_name=None,
show=True,
filename=None,
n_grid=50,
contour_levels=10,
dpi=200,
title='',
figsize=(12, 6),
use_min=False,
use_max=False,
aspect_equal=True,
legend_fontsize=12,
cmap='viridis',
X_points=None,
y_points=None,
plot_points=True,
points_color='white',
points_size=30,
point_color_below='blue',
point_color_above='red',
atol=1e-06,
plot_3d=False,
)Generate 2D contour and optionally 3D surface plots for a model’s predictions.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | object | A model with a predict method. | required |
| lower | array_like |
Lower bounds for each dimension. | required |
| upper | array_like |
Upper bounds for each dimension. | required |
| i | int | Index for the x-axis dimension. | 0 |
| j | int | Index for the y-axis dimension. | 1 |
| min_z | float | Min value for color scaling. Defaults to None. | None |
| max_z | float | Max value for color scaling. Defaults to None. | None |
| var_type | list | Variable types for each dimension. Defaults to None. | None |
| var_name | list | Variable names for labeling axes. Defaults to None. | None |
| show | bool | Whether to display the plot. Defaults to True. | True |
| filename | str | File path to save the figure. Defaults to None. | None |
| n_grid | int | Resolution for each axis. Defaults to 50. | 50 |
| contour_levels | int | Number of contour levels. Defaults to 10. | 10 |
| dpi | int | DPI for saving. Defaults to 200. | 200 |
| title | str | Title for the figure. Defaults to ““. | '' |
| figsize | tuple | Figure size. Defaults to (12, 6). | (12, 6) |
| use_min | bool | If True, leftover dims are set to lower bounds. | False |
| use_max | bool | If True, leftover dims are set to upper bounds. | False |
| aspect_equal | bool | Whether axes have equal scaling. Defaults to True. | True |
| legend_fontsize | int | Font size for labels and legends. Defaults to 12. | 12 |
| cmap | str | Colormap. Defaults to “viridis”. | 'viridis' |
| X_points | ndarray |
Original data points. Shape: (N, D). | None |
| y_points | ndarray |
Original target values. Shape: (N,). | None |
| plot_points | bool | Whether to plot X_points. Defaults to True. | True |
| points_color | str | Fallback color for data points. Defaults to “white”. | 'white' |
| points_size | int | Marker size for data points. Defaults to 30. | 30 |
| point_color_below | str | Color if actual z < predicted z. Defaults to “blue”. | 'blue' |
| point_color_above | str | Color if actual z >= predicted z. Defaults to “red”. | 'red' |
| atol | float | Absolute tolerance for comparing actual and predicted z-values. Defaults to 1e-6. | 1e-06 |
| plot_3d | bool | Whether to plot the 3D surface plot. Defaults to False. | False |
Returns
| Name | Type | Description |
|---|---|---|
(fig, (ax_contour, ax_surface)) |
Figure and axes for the contour and surface plots. |
simple_contour
plot.contour.simple_contour(
fun,
min_x=-1,
max_x=1,
min_y=-1,
max_y=1,
min_z=None,
max_z=None,
n_samples=100,
n_levels=30,
)Simple contour plot
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| fun | type |
description | required |
| min_x | int | description. Defaults to -1. | -1 |
| max_x | int | description. Defaults to 1. | 1 |
| min_y | int | description. Defaults to -1. | -1 |
| max_y | int | description. Defaults to 1. | 1 |
| min_z | int | description. Defaults to 0. | None |
| max_z | int | description. Defaults to 1. | None |
| n_samples | int | description. Defaults to 100. | 100 |
| n_levels | int | description. Defaults to 5. | 30 |
Returns
| Name | Type | Description |
|---|---|---|
| None | None |
Examples
>>> import matplotlib.pyplot as plt
import numpy as np
# from spotpython.fun.objectivefunctions import analytical
# fun = analytical().fun_branin
# simple_contour(fun=fun, n_levels=30, min_x=-5, max_x=10, min_y=0, max_y=15)