plot_manager
frequenz.lib.notebooks.solar.maintenance.plot_manager ¤
Plot Manager Module.
This module provides the PlotManager class, which is designed to manage the creation and layout of matplotlib figures and axes. It offers an organized and extensible way to create complex plot layouts with both simple subplots and advanced GridSpec layouts.
The PlotManager class allows users to: - Create figures with specified rows and columns of subplots. - Create figures with GridSpec layouts for advanced subplot arrangements. - Retrieve specific axes for plotting. - Update legends for figures based on specified axes and configurations. - Apply predefined plot styles to the figures. - Display and save all managed figures.
Classes¤
frequenz.lib.notebooks.solar.maintenance.plot_manager.PlotManager ¤
A class to manage the creation and layout of matplotlib figures and axes.
ATTRIBUTE | DESCRIPTION |
---|---|
figures |
A dictionary to store figure handles. |
axes |
A dictionary to store axes handles. |
current_style_params |
A dictionary to store the current style parameters. |
METHOD | DESCRIPTION |
---|---|
apply_plot_theme |
Apply a predefined style to the plots. |
create_figure |
Create a new figure with the specified number of rows and columns of subplots. |
create_multiple_figures |
Create multiple figures with the specified parameters. |
create_gridspec_figure |
Create a new figure with a GridSpec layout. |
create_multiple_gridspec_figures |
Create multiple GridSpec figures with the specified parameters. |
update_legend |
Update legend for a matplotlib figure based on specified axes and additional configurations. |
get_style_attribute |
Retrieve a specific style attribute. |
get_all_style_attributes |
Retrieve all current style attributes. |
get_axes |
Retrieve the axes for a given figure and axis index. |
get_figure |
Retrieve the figure handle for a given figure ID. |
show_all |
Display all the figures managed by PlotManager. |
save_all |
Save all the figures managed by PlotManager to the specified directory. |
manage_figure |
Context manager to handle showing and optionally saving figures automatically. |
Example usage
plot_manager = PlotManager()
Create a simple figure with 1 row and 2 columns of subplots¤
plot_manager.create_figure('fig1', nrows=1, ncols=2)
Retrieve the axes for the subplots and plot data¤
ax1 = plot_manager.get_axes('fig1', 0) ax2 = plot_manager.get_axes('fig1', 1)
ax1.plot([1, 2, 3], [4, 5, 6]) ax1.set_title('Plot 1')
ax2.plot([3, 2, 1], [6, 5, 4]) ax2.set_title('Plot 2')
Display all figures¤
plot_manager.show_all()
Save all figures to the 'plots' directory¤
plot_manager.save_all('plots')
Create a figure with a GridSpec layout¤
plot_manager.create_gridspec_figure('fig2', nrows=2, ncols=2, gridspec_kwargs=dict(height_ratios=[1, 2], width_ratios=[2, 1]))
Display all figures¤
plot_manager.show_all()
Save all figures to the 'plots' directory¤
plot_manager.save_all('plots')
Creating multiple figures without context manager¤
fig_params = [ {'fig_id': 'fig1', 'nrows': 1, 'ncols': 2, 'figsize': (10, 6)}, {'fig_id': 'fig2', 'nrows': 2, 'ncols': 2, 'figsize': (12, 8)} ] plot_manager.create_multiple_figures(fig_params)
Using context manager without saving¤
with plot_manager.manage_figure('fig3'): plot_manager.create_figure('fig3', nrows=1, ncols=2) ax1 = plot_manager.get_axes('fig3', 0) ax2 = plot_manager.get_axes('fig3', 1)
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Plot 1')
ax2.plot([3, 2, 1], [6, 5, 4])
ax2.set_title('Plot 2')
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 |
|
Functions¤
__init__ ¤
__init__(theme: str = 'frequenz-neustrom')
Initialize a PlotManager instance and optionally apply a plot style.
PARAMETER | DESCRIPTION |
---|---|
theme
|
The name of the plot style to apply.
TYPE:
|
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
adjust_axes_spacing ¤
Adjust the spacing between axes to have specified pixel spacing.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
pixels
|
The spacing between axes in pixels.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the figure ID is not found. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
apply_plot_theme ¤
apply_plot_theme(theme: str) -> None
Apply a predefined style to the plots.
PARAMETER | DESCRIPTION |
---|---|
theme
|
The name of the plot theme to apply.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the specified theme is not recognized |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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 |
|
create_figure ¤
create_figure(
fig_id: str,
nrows: int = 1,
ncols: int = 1,
figsize: tuple[int, int] = (10, 6),
) -> tuple[Figure, list[Axes]]
Create a new figure with the specified number of rows and columns of subplots.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
nrows
|
Number of rows of subplots.
TYPE:
|
ncols
|
Number of columns of subplots.
TYPE:
|
figsize
|
Size of the figure. |
RETURNS | DESCRIPTION |
---|---|
tuple[Figure, list[Axes]]
|
The created figure and axes. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
|
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
create_gridspec_figure ¤
create_gridspec_figure(
*,
fig_id: str,
nrows: int,
ncols: int,
figsize: tuple[int, int] = (10, 6),
gridspec_kwargs: dict[str, Any] | None = None
) -> None
Create a new figure with a GridSpec layout.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
nrows
|
Number of rows in the GridSpec layout.
TYPE:
|
ncols
|
Number of columns in the GridSpec layout.
TYPE:
|
figsize
|
Size of the figure. |
gridspec_kwargs
|
Additional keyword arguments for GridSpec. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the figure with the given ID already exists. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
create_multiple_figures ¤
create_multiple_figures(
fig_params: (
list[dict[str, str]]
| list[dict[str, int]]
| list[dict[str, tuple[int, int]]]
)
) -> None
Create multiple figures with the specified parameters.
PARAMETER | DESCRIPTION |
---|---|
fig_params
|
List of dictionaries, each containing parameters for creating a figure.
TYPE:
|
Example
fig_params = [ {'fig_id': 'fig1', 'nrows': 1, 'ncols': 2, 'figsize': (10, 6)}, {'fig_id': 'fig2', 'nrows': 2, 'ncols': 2, 'figsize': (12, 8)} ]
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
create_multiple_gridspec_figures ¤
create_multiple_gridspec_figures(
fig_params: (
list[dict[str, str]]
| list[dict[str, int]]
| list[dict[str, tuple[int, int]]]
| list[dict[str, dict[str, Any]]]
)
) -> None
Create multiple GridSpec figures with the specified parameters.
PARAMETER | DESCRIPTION |
---|---|
fig_params
|
List of dictionaries, each containing parameters for creating a GridSpec figure.
TYPE:
|
Example
fig_params = [ { "fig_id": "fig1", "nrows": 2, "ncols": 2, "figsize": (10, 6), "gridspec_kwargs": { "height_ratios": [1, 2], "width_ratios": [2, 1], } }, {"fig_id": "fig2", "nrows": 3, "ncols": 3, "figsize": (15, 10)}, ]
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
get_all_style_attributes ¤
get_axes ¤
Retrieve the axes for a given figure and axis index.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
ax_idx
|
Index of the axis to retrieve. If None, return all axes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[Axes]
|
The requested axis or axes as a list. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the figure does not exist. |
IndexError
|
If the axis index is out of bounds. |
TypeError
|
If the axes are not stored in a list. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
get_figure ¤
get_figure(fig_id: str) -> Figure
Retrieve the figure handle for a given figure ID.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Figure
|
The requested figure. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the figure does not exist. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
get_style_attribute ¤
manage_figure ¤
manage_figure(
fig_id: str,
save: bool = False,
directory: str | None = None,
) -> Generator[None, None, None]
Context manager to handle showing and optionally saving figures automatically.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
save
|
Whether to save the figures after showing them.
TYPE:
|
directory
|
Directory to save the figures if save is True.
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
None
|
The context within which the figure is managed.
TYPE::
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If save is True and directory is not specified. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
save_all ¤
save_all(directory: str) -> None
Save all the figures managed by PlotManager to the specified directory.
PARAMETER | DESCRIPTION |
---|---|
directory
|
Directory to save the figures.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the directory is not specified. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
show_all ¤
Display all the figures managed by PlotManager.
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If there are no figures to display. |
Source code in frequenz/lib/notebooks/solar/maintenance/plot_manager.py
update_legend ¤
update_legend(
fig_id: str,
axs: list[Axes],
on: str = "axes",
modifications: ModificationType | None = None,
**legend_kwargs: Any
) -> None
Update legend for a matplotlib figure or its axes.
PARAMETER | DESCRIPTION |
---|---|
fig_id
|
Identifier for the figure.
TYPE:
|
axs
|
A matplotlib Axes object or a list of Axes from which to collect handles and labels.
TYPE:
|
on
|
Specify whether to update legends on 'axes' or 'figure'.
TYPE:
|
modifications
|
A dictionary containing modifications: - 'additional_items': List of tuples (handle, label) to add to legends. For on == 'axes', this should be a list of lists corresponding to each axis. For on == 'figure', this should be a list of tuples and all will be added to the figure legend. - 'remove_label': A label to remove from legends. - 'replace_label': A dictionary mapping old labels to new labels.
TYPE:
|
**legend_kwargs
|
Additional keyword arguments for the legend function.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the figure ID is not found or if inputs are invalid. |
Example
modifications = { 'additional_items': [ [(handle1, 'New Label 1')], [(handle2, 'New Label 2')] ], 'remove_label': 'Old Label', 'replace_label': {'Old Label': 'New Label'}, } plot_manager.update_legend( 'fig1', [ax1, ax2], on='axes', modifications=modifications, loc='upper right' )