Density Plot Strategy¶
density_plot_strategy ¶
Density Plot Strategy.
This module implements the DensityPlotStrategy class following the Strategy Pattern, providing specific logic for generating overlaid density distribution visualizations with flexible configuration.
Classes:
| Name | Description |
|---|---|
DensityPlotStrategy | Concrete strategy for density plot generation |
Notes
This strategy supports: - Overlaid density curves for multiple groups - Semi-transparent fill under curves - Probability density estimation (KDE) - Custom styling from YAML configuration - Horizontal legend below chart
Technical Details: - Uses Plotly's figure_factory.create_distplot for KDE - Supports grouping by categorical variables - Hides histogram and rug plot (shows only density curves) - Auto-configures legend positioning - Applies opacity for visual clarity when curves overlap
For supported use cases, refer to the official documentation.
Classes¶
DensityPlotStrategy ¶
Bases: BasePlotStrategy
Density plot strategy for distribution visualization.
This strategy creates overlaid probability density curves to visualize and compare distributions across multiple groups.
The density plot uses Kernel Density Estimation (KDE) to show smooth probability distributions, making it ideal for comparing continuous distributions across categorical groups.
Attributes:
| Name | Type | Description |
|---|---|---|
data_config | Dict[str, Any] | Data processing configuration from YAML |
plotly_config | Dict[str, Any] | Plotly-specific configuration from YAML |
Notes
Configuration Structure (YAML): visualization: strategy: "DensityPlotStrategy" plotly: # Required: Data configuration value_column: "toxicity_score" # Numeric column for density group_column: "endpoint" # Categorical grouping column
# Optional: Chart styling
chart:
title:
text: "Distribution Plot"
show_hist: false # Hide histogram bars
show_rug: false # Hide rug plot
fill: "tozeroy" # Fill under curve
opacity: 0.5 # Curve transparency
# Optional: Layout configuration
layout:
template: "simple_white" # Plotly template
height: 800 # Chart height
width: 1200 # Chart width
xaxis:
title: "Value"
yaxis:
title: "Probability Density"
gridcolor: "lightgray"
legend:
orientation: "h" # Horizontal legend
yanchor: "bottom"
y: 0 # Legend position
xanchor: "center"
x: 0.5
title_text: "Group"
Refer to the official documentation for supported use cases and detailed configuration examples.
See Also
BasePlotStrategy : Abstract base class plotly.figure_factory.create_distplot : Plotly density plot documentation
Initialize density plot strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | Dict[str, Any] | Complete configuration from YAML file | required |
Source code in src/domain/plot_strategies/charts/density_plot_strategy.py
Functions¶
validate_data ¶
Validate input data for density plot requirements.
Validation rules applied: - DataFrame not empty - Required columns exist (value_column, group_column) - value_column contains numeric data - At least one group exists - Each group has sufficient data points for KDE
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Input data to validate | required |
Raises:
| Type | Description |
|---|---|
ValueError | If any validation rule fails |
Source code in src/domain/plot_strategies/charts/density_plot_strategy.py
process_data ¶
Process data for density plot visualization.
Processing steps: 1. Extract configuration (value_column, group_column) 2. Remove NaN values from both columns 3. Sort groups alphabetically for consistent ordering 4. Filter out groups with insufficient data points 5. Return cleaned DataFrame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Validated input data | required |
Returns:
| Type | Description |
|---|---|
DataFrame | Processed data ready for density plot (contains only rows with valid value and group data) |
Source code in src/domain/plot_strategies/charts/density_plot_strategy.py
create_figure ¶
Create Plotly density plot from processed data.
Creates overlaid density curves using figure_factory.create_distplot: - One curve per group - Semi-transparent fill under curves - Kernel Density Estimation (KDE) for smooth curves - Horizontal legend below chart - No histogram or rug plot
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processed_df | DataFrame | Processed data with value and group columns | required |
Returns:
| Type | Description |
|---|---|
Figure | Configured Plotly figure with density plot |
Notes
Chart Configuration: - Overlaid KDE curves - Fill to zero (tozeroy) - Configurable opacity for overlaps - Horizontal legend - Grid lines on y-axis
Source code in src/domain/plot_strategies/charts/density_plot_strategy.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
apply_filters ¶
Apply filters to data.
This is a common implementation that can be overridden by subclasses if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Data to filter. | required |
filters | Optional[Dict[str, Any]] | Filter specifications. | None |
Returns:
| Type | Description |
|---|---|
DataFrame | Filtered data. |
Source code in src/domain/plot_strategies/base/base_plot_strategy.py
apply_customizations ¶
Apply custom styling to figure.
This is a hook for future customization features (FLEXIVEL and FLEXIVEL2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig | Figure | Base figure. | required |
customizations | Optional[Any] | Customization specifications. | None |
Returns:
| Type | Description |
|---|---|
Figure | Customized figure. |
Source code in src/domain/plot_strategies/base/base_plot_strategy.py
generate_plot ¶
generate_plot(data: DataFrame, filters: Optional[Dict[str, Any]] = None, customizations: Optional[Any] = None) -> go.Figure
Generate complete plot (Template Method).
This method orchestrates the entire plot generation process: 1. Validate input data 2. Process data 3. Apply filters 4. Create figure 5. Apply customizations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | Input data. | required |
filters | Optional[Dict[str, Any]] | Filters to apply. | None |
customizations | Optional[Any] | Customizations to apply. | None |
Returns:
| Type | Description |
|---|---|
Figure | Complete Plotly figure. |
Raises:
| Type | Description |
|---|---|
ValueError | If validation fails. |