3D Magnetic Field Plot

Plots the data of a data set given the magnetic field. The components of the plot are the original magnetic field represented with a black cage, the distorted magnetic plot as an orange cage, and the samples of the magnetic field as blue dots.

Parameters:

Name Type Description Default
soft_iron ndarray

Soft-iron matrix as a (3, 3) numpy array.

required
hard_iron ndarray

Hard-iron matrix as a (3, 1) numpy array.

required
magnetic_field ndarray

Magnetic field in G as a (n, 3) numpy array.

required
local_magnetic_field ndarray

Local magnetic field in G as a (3, ) numpy array.

required
save str

Directory to save the plots as a string.

''
Source code in magyc/plots/plots.py
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
def magfield_data_plot(soft_iron: np.ndarray, hard_iron: np.ndarray, magnetic_field: np.ndarray,
                       local_magnetic_field: np.ndarray, save: str = "") -> None:
    """
    Plots the data of a data set given the magnetic field. The components of the
    plot are the original magnetic field represented with a black cage, the
    distorted magnetic plot as an orange cage, and the samples of the magnetic
    field as blue dots.

    Args:
        soft_iron (np.ndarray): Soft-iron matrix as a (3, 3) numpy array.
        hard_iron (np.ndarray): Hard-iron matrix as a (3, 1) numpy array.
        magnetic_field (np.ndarray): Magnetic field in G as a (n, 3) numpy array.
        local_magnetic_field (np.ndarray): Local magnetic field in G as a (3, ) numpy array.
        save (str): Directory to save the plots as a string.
    """
    # Sphere radius
    r = np.linalg.norm(local_magnetic_field)

    # Create Figure
    fig = plt.figure(figsize=(9, 9))
    ax = fig.add_subplot(111, projection="3d")

    # Remove background and axes
    fig.patch.set_facecolor('none')
    ax.set_axis_off()
    ax.spines['top'].set_color('none')
    ax.spines['bottom'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.set_xticks([])
    ax.set_yticks([])

    # Get color palette
    colors = _get_color_palette()

    # Plot the magnetic field measurements with decreased amount of samples
    magnetic_field = magnetic_field[::5, :]
    ax.scatter(magnetic_field[:, 0], magnetic_field[:, 1], magnetic_field[:, 2], marker=".",
               color=colors["Midnight Green"])

    # Plot the disturbed magnetic field
    ellipsoid_plot(hard_iron.flatten(), [r, r, r], soft_iron, ax=ax, plot_axes=True,
                   cage_color=colors["Alloy Orange"], cage_alpha=0.4)

    # Plot the corrected magnetic field
    ellipsoid_plot([0, 0, 0], [r, r, r], np.eye(3), ax=ax, plot_axes=True, cage_color=colors["Rich Black"])

    # Set axes fro equal ratios
    ax.set_box_aspect([1, 1, 1])
    limits = np.array([ax.get_xlim3d(), ax.get_ylim3d(), ax.get_zlim3d()])
    x, y, z = np.mean(limits, axis=1)
    radius = 0.29 * np.max(np.abs(limits[:, 1] - limits[:, 0]))
    ax.set_xlim3d([x - radius, x + radius])
    ax.set_ylim3d([y - radius, y + radius])
    ax.set_zlim3d([z - radius, z + radius])

    # Set view angle to 210, best angle to see the ellipsoid with respect to the sphere.
    ax.view_init(30, 210)

    # Show the plot or save the plot given the save parameter.
    plt.tight_layout()
    if save:
        # plt.savefig(save, format="pdf")
        plt.savefig(".".join([save.split(".")[0], "png"]))
    else:
        plt.show()
    plt.close()