Skip to main content

interactive marker support for matplotlib

Project description

mpl-markers

Interactive data markers for line plots in matplotlib

Installation

pip install mpl-markers

Usage

import mpl_markers as mplm

Line Markers

Add a marker attached to matplotlib data lines:

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('ggplot')
plt.rc('font', size=7)

fig, ax = plt.subplots(1,1)
x1 = np.linspace(-2*np.pi, 2*np.pi, 1000)

ax.plot(x1, np.sin(x1)*np.cos(x1)**2)

mplm.line_marker(x=0)

In interactive matplotlib backends (i.e. QtAgg), the marker can be dragged to any location along the data line, or moved incrementally with the left/right arrow keys. Interactive markers are not supported for static inline figures generated in Jupyter Notebooks.

example1

Additional markers can be added by using Shift+Left Mouse button. The active marker can be removed from the plot by pressing the Delete key.

Axis Markers

Add an axis marker that moves freely on the canvas:

ax.xaxis.set_major_formatter(lambda x, pos: '{:.2f}$\pi$'.format(x/np.pi))
mplm.axis_marker(x=0, y=-0.2)

example2

set_major_formatter will set the formatting for the axes ticks and the marker label. To only format the label, use the following,

mplm.axis_marker(x=0, y=-0.2, xformatter="{:.2f}$\pi$", yformatter="{:.2f}$\pi$")

Meshgrid Markers

Data markers can also be added to pcolormesh plots. The marker label shows the value of the color-mapped z data.

xy = np.linspace(-1, 1, 100)
x, y = np.meshgrid(xy, xy)
z = np.sin(2*x)**2 + np.cos(3*y)**2

fig, ax = plt.subplots(1, 1)
m = ax.pcolormesh(x, y, z, vmin=0, vmax=2)
plt.colorbar(m)

# add a data marker at a single x/y point on the plot. x/y is in data coordinates.
mplm.mesh_marker(x=0.75, y=0)

example3

Styling

The marker style is controlled by the mpl_markers/style/default.json file:

{
    "xline": {
        "linewidth": 0.6,
        "color": "k",
        "linestyle": "dashed"
    },
    "yline": {
        "linewidth": 0.6,
        "color": "k",
        "linestyle": "dashed"
    },
    "xlabel": {
        "fontsize": 8,
        "color": "black",
        "bbox": {
            "boxstyle": "square",
            "facecolor": "white",
            "edgecolor": "black",
            "alpha": 1,
            "linewidth": 1.5
        }
    },
    "ylabel": {
        "fontsize": 8,
        "bbox": {
            "boxstyle": "square",
            "facecolor": "white",
            "edgecolor": "black",
            "alpha": 1,
            "linewidth": 1.5
        }
    },
    "xydot": {
        "markersize": 10,
        "marker": "."
    },
    "xymark": {
        "markersize": 10,
        "marker": ".",
        "markerfacecolor":"white", 
        "markeredgecolor":"k"
    }
}

To use custom styles, pass in a dictionary of artist settings when creating the marker that matches the keys in this file. For example, this line will change the color of the dotted vertical line to red.

mplm.line_marker(x=0, xline=dict(color='red', linestyle="dashed", alpha=0.5))

To turn on/off any of the artists, pass in True/False for the artist key,

mplm.line_marker(x=0, xlabel=True, xline=False)

License

mpl-markers is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpl_markers-0.0.8.tar.gz (21.6 kB view hashes)

Uploaded Source

Built Distribution

mpl_markers-0.0.8-py3-none-any.whl (19.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page