Plotting¶
Matplotlib bar-plot helpers that consume a Result instance and
visualise earth potential rise, branch currents and bus currents
either for a specific frequency or as RMS across all frequencies.
Physical / modelling context¶
A fault calculation produces three families of derived quantities that are routinely inspected:
- Earth potential rise (EPR) — the magnitude of \(\underline{u}(f)\) per bus, both per frequency and as the RMS \(\sqrt{\sum_f |u(f)|^2}\) across all frequencies. The RMS view is the touch-voltage proxy under EN 50522.
- Branch currents — the per-frequency phasor of the current flowing through each grounding-conductor branch, signed in the branch traversal direction. This is the shield-current pattern that determines reduction factor and EMC effects.
- Bus currents — the injected current per bus, i.e. the residual that returns through the local earth path \(1/Z_E\). The sum over all buses equals the fault current scaled by \(r\).
For the time-domain transient solver
(groundinsight.simulation.transient) the same
plot helpers are mirrored with a _transient suffix and consume
a ResultTransient instead.
Example¶
import groundinsight as gi
# Assume `net` has been solved via gi.run_fault(...).
result = net.results["fault1"]
# EPR bar plot at 50 Hz and as RMS over all frequencies
gi.plot_bus_voltages(result=result, frequencies=[50.0],
title="EPR @ 50 Hz")
gi.plot_bus_voltages(result=result,
title="EPR (RMS over all frequencies)")
# Branch and bus currents
gi.plot_branch_currents(result=result, frequencies=[50.0],
title="Shield I")
gi.plot_bus_currents(result=result, title="Bus injection (RMS)")
# Transient counterpart (after a TransientStudy.solve(...))
gi.plot_epr_transient(result=result_t, title="EPR transient")
gi.plot_branch_current_transient(result=result_t, title="Shield I(t)")
All helpers return the matplotlib.figure.Figure they create, so
they integrate cleanly with notebook display and with
fig.savefig(...) calls.
Only computed frequencies can be plotted¶
frequencies= selects from the frequencies the result actually
contains; asking for one that was never computed raises a
KeyError naming both the requested and the available values:
net = gi.create_network(name="net", frequencies=[50.0])
...
gi.plot_bus_voltages(result=result, frequencies=[250.0])
# KeyError: Frequency [250.0] not present in 'uepr_freq' of any bus;
# the result was computed for [50.0] Hz. ...
This is not pedantry about arguments. A missing frequency used to
be substituted with 0.0, and a bar of height zero on an EPR plot
is a statement: "the fifth harmonic causes no earth potential
rise at this station". Nothing in the figure distinguished that
from "250 Hz was never part of the calculation". A bar of height
zero now always means a measured zero.
The same check covers the partial case — a frequency present on some buses and missing on others — because a bar group that mixes measured values with substituted zeros is worse still.
Figure ownership¶
By default every helper creates its figure through pyplot and
leaves it open; the caller owns it. In a notebook that is what you
want. In a loop — a soil-resistivity sweep, one plot per scenario —
it is not, and matplotlib warns after the twentieth figure. Pass
close=True and the helper releases the figure it created before
returning:
for rho in (50.0, 100.0, 500.0, 1000.0):
...
fig = gi.plot_bus_voltages(result=result, title=f"rho = {rho}",
close=True)
fig.savefig(f"epr_{rho:.0f}.png")
The returned figure is still complete — every axis, bar and label
is on it, and savefig works exactly as before. close=True only
unregisters it from pyplot, so it is collected once the last
reference goes out of scope. plt.close(fig) after the call
remains equivalent.
Drawing into an existing axis¶
Pass ax= to draw into an axis you created yourself. The helper
then leaves the surrounding figure alone: it applies no
tight_layout, closes nothing, and returns your figure rather
than a new one. This is what makes two scenarios comparable side
by side:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(16, 5), sharey=True)
gi.plot_bus_voltages(result=base, ax=axes[0], title="base case")
gi.plot_bus_voltages(result=outage, ax=axes[1], title="cable out")
fig.tight_layout()
Two combinations are rejected rather than silently ignored:
| Combination | Why it raises |
|---|---|
ax= with figsize= |
The figure already exists and may hold other panels, so the size cannot be honoured. Size it yourself: plt.subplots(figsize=...), or ax.figure.set_size_inches(...). |
ax= with close=True |
close= releases the figure this call created. With ax= the figure belongs to you, and closing it would take every sibling panel with it. |
A figsize that cannot be used raises as well, rather than being
replaced by the default: matplotlib accepts figsize=(0, 0) when
the figure is created and only fails much later, when it is drawn
or saved, so the traceback would point at savefig instead of at
the call responsible.
API reference¶
plotting ¶
Plotting Module.
This module provides functions for visualizing the results of electrical network calculations, including UEPR (Earth Potential Rise) for buses and branch currents. It utilizes Matplotlib to generate plots that can display both frequency-dependent and RMS (Root Mean Square) values for various electrical parameters. These visualizations aid in analyzing the performance and behavior of the electrical network under different fault conditions.
Every helper accepts two optional arguments that control where the plot is drawn and who owns the resulting figure:
ax
Draw into an existing :class:matplotlib.axes.Axes instead of creating a
figure. This is what makes multi-panel comparisons possible -- one panel
per outage scenario, per soil resistivity, per fault location -- and
follows the same convention as pandas.DataFrame.plot(ax=...).
close
Close the freshly created figure before returning it. The figure object
remains fully usable (fig.savefig(...) still works); it is merely no
longer held by pyplot's figure manager, which is what a long
parameter sweep needs in order not to accumulate figures until
matplotlib warns at twenty.
Omitting both reproduces the historical behaviour exactly: a new figure of
the helper's default size, registered with pyplot and left open.
plot_branch_current_transient ¶
plot_branch_current_transient(
result: ResultTransient,
*,
branches: Optional[List[str]] = None,
figsize: Optional[Tuple[float, float]] = None,
title: str = "Branch current over time",
show: bool = False,
ax: Optional[plt.Axes] = None,
close: bool = False
) -> plt.Figure
Plot the time-domain shield current for one or more observed branches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ResultTransient
|
A transient result. |
required |
branches
|
list of str
|
Restrict the plot to a subset of the observed branches. |
None
|
figsize
|
tuple of (float, float)
|
Figure size in inches. |
None
|
title
|
str
|
Plot title. |
'Branch current over time'
|
show
|
bool
|
Call |
False
|
ax
|
Axes
|
Draw into this axis instead of creating a new figure. |
None
|
close
|
bool
|
Close the created figure before returning it; the figure object
stays usable. Cannot be combined with |
False
|
Returns:
| Type | Description |
|---|---|
Figure
|
The generated figure, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
Without ax and without close the returned figure is registered
with pyplot and stays open until the caller closes it; pass
close=True when plotting in a loop.
Source code in src/groundinsight/plotting.py
plot_branch_currents ¶
plot_branch_currents(
result: Result,
frequencies: Optional[List[float]] = None,
figsize: Optional[Tuple[float, float]] = None,
title: str = "Branch Currents",
yscale: str = "linear",
show: bool = False,
*,
ax: Optional[plt.Axes] = None,
close: bool = False
) -> plt.Figure
Plot the branch currents for each branch.
Generates a bar plot of branch currents in the network. It can plot either frequency-dependent current magnitudes or RMS current values based on the provided parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The :class: |
required |
frequencies
|
list of float
|
Frequencies (in Hz) to plot. If |
None
|
figsize
|
tuple of (float, float)
|
Figure size in inches. |
None
|
title
|
str
|
Title of the plot. Defaults to |
'Branch Currents'
|
yscale
|
(linear, log)
|
Scale for the y-axis. Defaults to |
'linear'
|
show
|
bool
|
Whether to display the plot immediately. Defaults to |
False
|
ax
|
Axes
|
Draw into this axis instead of creating a new figure. Keyword-only. |
None
|
close
|
bool
|
Close the created figure before returning it; the figure object
stays usable. Cannot be combined with |
False
|
Returns:
| Type | Description |
|---|---|
Figure
|
The Matplotlib figure object containing the plot. With |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a specified frequency is not present in |
ValueError
|
If |
Notes
Without ax and without close the returned figure is registered
with pyplot and stays open until the caller closes it; pass
close=True when plotting in a loop.
Examples:
>>> import groundinsight as gi
>>> fig = gi.plot_branch_currents(
... result=result, frequencies=[50, 60],
... )
Source code in src/groundinsight/plotting.py
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 | |
plot_bus_currents ¶
plot_bus_currents(
result: Result,
frequencies: Optional[List[float]] = None,
figsize: Optional[Tuple[float, float]] = None,
title: str = "Bus Currents",
yscale: str = "linear",
show: bool = False,
*,
ax: Optional[plt.Axes] = None,
close: bool = False
) -> plt.Figure
Plot the bus currents for each bus.
Generates a bar plot of bus currents in the network. It can plot either frequency-dependent current magnitudes or RMS current values based on the provided parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The :class: |
required |
frequencies
|
list of float
|
Frequencies (in Hz) to plot. If |
None
|
figsize
|
tuple of (float, float)
|
Figure size in inches. |
None
|
title
|
str
|
Title of the plot. Defaults to |
'Bus Currents'
|
yscale
|
(linear, log)
|
Scale for the y-axis. Defaults to |
'linear'
|
show
|
bool
|
Whether to display the plot immediately. Defaults to |
False
|
ax
|
Axes
|
Draw into this axis instead of creating a new figure. Keyword-only. |
None
|
close
|
bool
|
Close the created figure before returning it; the figure object
stays usable. Cannot be combined with |
False
|
Returns:
| Type | Description |
|---|---|
Figure
|
The Matplotlib figure object containing the plot. With |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a specified frequency is not present in |
ValueError
|
If |
Notes
Without ax and without close the returned figure is registered
with pyplot and stays open until the caller closes it; pass
close=True when plotting in a loop.
Examples:
>>> import groundinsight as gi
>>> fig = gi.plot_bus_currents(
... result=result, frequencies=[50, 60],
... )
Source code in src/groundinsight/plotting.py
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 | |
plot_bus_voltages ¶
plot_bus_voltages(
result: Result,
frequencies: Optional[List[float]] = None,
figsize: Optional[Tuple[float, float]] = None,
title: str = "UEPR vs Bus Name",
yscale: str = "linear",
show: bool = False,
*,
ax: Optional[plt.Axes] = None,
close: bool = False
) -> plt.Figure
Plot the UEPR (Earth Potential Rise) for each bus.
Generates a bar plot of UEPR values for each bus in the network. It can plot either frequency-dependent UEPR magnitudes or RMS UEPR values based on the provided parameters. Additionally, the y-axis scale can be linear or logarithmic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The :class: |
required |
frequencies
|
list of float
|
Frequencies (in Hz) to plot. If |
None
|
figsize
|
tuple of (float, float)
|
Figure size in inches as a |
None
|
title
|
str
|
Title of the plot. Defaults to |
'UEPR vs Bus Name'
|
yscale
|
(linear, log)
|
Scale for the y-axis. Defaults to |
'linear'
|
show
|
bool
|
Whether to display the plot immediately. If |
False
|
ax
|
Axes
|
Draw into this axis instead of creating a new figure. The figure
that owns |
None
|
close
|
bool
|
Close the created figure with |
False
|
Returns:
| Type | Description |
|---|---|
Figure
|
The Matplotlib figure object containing the plot. With |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a specified frequency is not present in |
ValueError
|
If |
Notes
Without ax and without close the returned figure is registered
with pyplot and stays open until the caller closes it. In a loop --
a parameter sweep over specific_earth_resistance, for instance --
pass close=True (or call plt.close(fig) yourself), otherwise
matplotlib accumulates the figures and warns after 20.
tight_layout is only applied to figures this call created; a
caller-supplied ax leaves the surrounding layout alone.
Examples:
>>> import groundinsight as gi
>>> fig = gi.plot_bus_voltages(
... result=result, frequencies=[50, 60], yscale="log",
... )
Two scenarios side by side in one figure:
>>> import matplotlib.pyplot as plt
>>> fig, axes = plt.subplots(1, 2, figsize=(16, 5), sharey=True)
>>> gi.plot_bus_voltages(result=base, ax=axes[0], title="base")
>>> gi.plot_bus_voltages(result=outage, ax=axes[1], title="cable out")
Source code in src/groundinsight/plotting.py
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 | |
plot_epr_transient ¶
plot_epr_transient(
result: ResultTransient,
*,
buses: Optional[List[str]] = None,
figsize: Optional[Tuple[float, float]] = None,
title: str = "EPR over time",
show: bool = False,
ax: Optional[plt.Axes] = None,
close: bool = False
) -> plt.Figure
Plot the time-domain EPR for one or more observed buses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ResultTransient
|
A result returned by
:meth: |
required |
buses
|
list of str
|
Restrict the plot to a subset of the observed buses. |
None
|
figsize
|
tuple of (float, float)
|
Figure size in inches. |
None
|
title
|
str
|
Plot title. Defaults to |
'EPR over time'
|
show
|
bool
|
Call |
False
|
ax
|
Axes
|
Draw into this axis instead of creating a new figure -- for example to stack EPR and shield current in one two-row figure. |
None
|
close
|
bool
|
Close the created figure before returning it; the figure object
stays usable. Cannot be combined with |
False
|
Returns:
| Type | Description |
|---|---|
Figure
|
The generated figure, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
Without ax and without close the returned figure is registered
with pyplot and stays open until the caller closes it; pass
close=True when plotting in a loop.
Examples:
>>> import matplotlib.pyplot as plt
>>> fig, (top, bottom) = plt.subplots(2, 1, sharex=True)
>>> gi.plot_epr_transient(result=res, ax=top)
>>> gi.plot_branch_current_transient(result=res, ax=bottom)
Source code in src/groundinsight/plotting.py
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 | |