简体   繁体   中英

How to store the output of a function

I have a dataframe as follows: x = [1,2,3.....10000] y = [1,2,3.....10000]

I have used SpanSelector tool in matplotlib to make a selection on x data. Based on the selection, I get two values (xmin, xmax)

now I want to plot another plot (different from the one I already have plotted) with x axis set to just (xmin, xmax)

`

ax.errorbar(x=x,y=y,yerr=y_err)

def onselect(xmin, xmax):

    print('\nLower value: ',xmin)
    print('Upper value: ',xmax)
    return xmin,xmax

span = SpanSelector(
    ax,
    onselect,
    "horizontal",
    useblit=True,
    props=dict(alpha=0.5, facecolor="tab:green"),
    interactive=True,
    drag_from_anywhere=True
)

`

I tried using a plt command inside the fuction, it printing the given print statements, but not plotting.

I bascially want this: https://matplotlib.org/stable/gallery/widgets/span_selector.html for my own usecase, which I am not able to do. Any other method to do the same would also suffice.

The problem is that the function you are passing to the SpanSelector is implemented as a callback, where the return value is not used. So you have to find a way for this function to store the values somewhere permanent (beyond the lifetime / scope of the function) without returning it. There are really two ways: Either, you use global variables and let the function write to them. This would look something like this:

xmin_global = None
xmax_global = None

def onselect(xmin, xmax):
    global xmin_global 
    global xmax_global 
    xmin_global = xmin
    xmax_global = xmax

However, the use of global variables is usually discouraged because there can be hard to track behavior and with larger code bases, it becomes really complicated to understand what the code is doing and to debug it. For small standalone code snippets however, it's usually okay to use it.

The other (and imo better) way to do it is to define onselect as a method of an object that holds the variables xmin and xmax as member attributes. Then the method can set these attributes of the object to your values and you can retrieve them afterwards from the object. This could look something like this:

class Selector:
    
    def __init__(self, ax):
        self.ax = ax
        self.xmin = None
        self.xmax = None
        
        self.span = SpanSelector(
            self.ax,
            self.onselect,
            "horizontal",
            useblit=True,
            props=dict(alpha=0.5, facecolor="tab:blue"),
            interactive=True,
            drag_from_anywhere=True
        )
        
    def onselect(self, xmin, xmax):
        self.xmin = xmin
        self.xmax = xmax
        
selector = Selector(ax1)

Here, I initialize the SpanSelector also inside the class in the __init__ method. But this could just as well be done outside the class. The main point is that onselect as a method has access to all the attributes of the object and can write to them through the self reference. They can be thought of like global variables inside the scope of the class. You can access them later using selector.xmin and selector.xmax .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM