vendredi 2 novembre 2018

Testing event handling and picking with matplotlib

As per subject, how to write tests for functions that deal with pick event handling in matplotlib?

In particular, given the following minimum working example, how to write a test that would offer 100% coverage?

import numpy as np
import matplotlib.pyplot as plt

def onpick(event):
    ind = event.ind
    print('you clicked on point(s):', ind)

def attach_handler_to_figure(figure):
    figure.canvas.mpl_connect('pick_event', onpick)

def main():
    plt.ion()
    x, y, c, s = np.random.rand(4, 100)
    fig, ax = plt.subplots()
    ax.scatter(x, y, 100*s, c, picker=True)
    attach_handler_to_figure(fig)

main()

The critical part is for me writing tests for the functions onpick and attach_handler_to_figure. Regarding the plotting I find this answer to be satisfying!

Aucun commentaire:

Enregistrer un commentaire