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 value at the fault bus is the earthing voltage \(U_E\) that EN 50522 compares with the permissible touch voltage; it is an upper bound of the touch voltage, not the touch voltage itself (see Compare results with EN 50522 limits).
- Branch currents — the current of the grounding conductor
(screen, earth wire) of each branch,
I_branch_A, counted positive fromfrom_bustoto_busfor the physical fault (see Signs and phase angles). This is the shield-current pattern behind the reduction factor and the EMC effects. - Bus currents — the electrode current of each bus,
I_bus_A: what the bus's own electrode \(Z_B\) passes into the soil. The electrode currents of all buses sum to zero — the fault current enters the soil at some stations and leaves it at others — so their sum says nothing about the reduction factor. The current of the earthing conductor at a source or fault bus,I_inj_A, is a different quantity; size an earthing conductor withI_inj_A, an electrode withI_bus_A(Thermal limits).
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¶
# fragment: assumes a network `net` solved for the fault "fault1" and a transient result `result_t`
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:
# fragment: the network and its solve are elided
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:
# fragment: the solve per rho is elided
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:
# fragment: assumes two results `base` and `outage`
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.
Import cost¶
matplotlib.pyplot is imported on the first call of a plotting helper, not
by import groundinsight. The package import is about a third faster
(median 1.33 s → 0.83 s measured on the development container), and a
program that never plots — a GUI that draws with its own charts, a server, a
batch job — never loads pyplot's global state (backend selection, figure
registry). Nothing changes for code that plots: the first helper call
imports pyplot, every later one finds it loaded.
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 gives a new figure of the helper's default size, registered
with pyplot and left open.
matplotlib.pyplot is imported on the first call of a helper, not when the
package is imported. A module-level import would cost every
import groundinsight about a third of its time (0.36 s of 1.1 s) and
put pyplot's global state -- backend selection, the figure registry -- into
every process, including a multi-threaded GUI or server that never draws with
matplotlib. The annotations below name plt.Axes / plt.Figure as
strings; plt exists for type checkers only, so
typing.get_type_hints on a helper needs matplotlib.pyplot imported
first.
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[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.
Examples:
result comes from :meth:TransientStudy.solve:
Source code in src/groundinsight/plotting.py
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 | |
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[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
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 | |
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[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
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 | |
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[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
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 | |
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[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
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 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | |