Skip to content

plot_fit customisation

Explanation of changes

Add customisability of plot parameters to visualization.mpl_plotting.plot_fit whilst default behaviour remains unchanged.

afbeelding

This behaviour is achieved with:

# Default behaviour
fig, ax = plt.subplots()
ax.set_title("Default fits")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.plot(x, cos_data, '.')
plot_fit(ax, cos_result)
ax.legend()

# Custom Behaviour
fig, ax = plt.subplots()
ax.set_title("Custom fits")
ax.set_xlabel("x")
ax.set_ylabel("y")
cos_data_line, = ax.plot(x, cos_data, '.')
sin_data_line, = ax.plot(x, sin_data, '.')
plot_fit(
    ax=ax,
    fit_res=cos_result,
    fit_kwargs={
        "c": cos_data_line.get_color(),
        "label": "Cosine Fit"
    },
    plot_init=False
)
plot_fit(
    ax=ax,
    fit_res=sin_result,                   
    fit_kwargs={                          # Plots are modified through dictionaries
        "c": sin_data_line.get_color(),   # Short mpl keywords work
        "label": "Sine Fit"               # The legend label is also specified in the keyword dictionary
    },
    plot_init=True,
    init_kwargs={                         # Init plot can be customised too
        "color": "black",                 # Long keywords work too
        "linestyle": ":",
        "label": "Sine guess"
    }
)
ax.legend()

N.B. Data and fits were generated using numpy and lmfit respectively

N.B. Requires the analysis.fitting_models.CosineModel from the branch cosine_guess (no merge request)

Motivation of changes

Color, linestyle and legend label hardcoded in current version of plot_fit. If used to plot multiple fits in one figure, the figure can be come hard to read.


Merge checklist

See also merge request guidelines

  • Merge request has been reviewed and approved by a project maintainer.
  • Merge request contains a clear description of the proposed changes and the issue it addresses.
  • Merge request made onto appropriate branch (main for most MRs).
  • New code is fully tested.
  • New code is documented and docstrings use numpydoc format.
  • CHANGELOG.rst and AUTHORS.rst have been updated (when applicable).
  • CI pipelines pass
    • pre-commit run --all-files --hook-stage commit passes (gitlab-ci),
    • test suite passes (gitlab-ci),
    • no degradation in code-coverage (codacy),
    • no (serious) new pylint code quality issues introduced (codacy),
    • documentation builds successfully (CI and readthedocs),
    • windows tests pass (manually triggered by maintainers before merging).

For reference, the issues workflow is described in the contribution guidelines.

Edited by Tim Vroomans

Merge request reports

Loading