Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An unusual Python syntax element frequently used in Matplotlib

One proviso: The syntax element at the heart of my Question is in the Python language; however, this element appears frequently in the Matplotlib library, which is the only context i have seen it. So whether it's a general Python syntax question or a library-specific one, i am not sure. What i do know is that i could not find anything on point--either in the Python Language Reference or in the Matplotlib docs.

Those who use and/or develop with the excellent Python plotting library, Matplotlib will recognize the syntax pattern below. (

from matplotlib import pyplot as MPL

>>> l, = MPL.plot(s, t)      # s & t are ordinary NumPy 1D arrays

What is the construction on the left-hand side of this expression? And,

what is the purpose for using it?

I am familiar with Python's assignment unpacking, e.g.,

>>> a, b = [100, 200]

I'm also aware that in Python one-item tuples are sometimes represented as t,

And either could be the answer to the first question above; if so then i don't yet understand the reason why only the first element of the value returned from the call to plot is needed here.

(note: "l" is a lower case "ell"; i am using this letter because ls is the letter most often used here, probably because it is bound to an object that begins with the same letter-see below).


Some additional context:

The call to plot returns a list of line2D instances:

>>> type(l)
  <class 'matplotlib.lines.Line2D'>

So l is an object of type line2D.

Once bound to the lines2D object, this "variable" is usually seen in Matplotlib code like so:

>>> l.set_color("orange")

This expression changes the color of the line that represents the data values inside the plot window (the "plot line")

Below is one more example; it shows a common scenario for this "variable-comma" construction, which is embedding small toolkit/graphics-backend-independent widgets in a Matplotlib plot window, e.g., to toggle on/off by checkbox, multiple data series appearing in the plot window.

In the code below, a simple Matplotlib plot and a simple widget comprised of two checkboxes one for each data series are created.

l0 and l1 are again bound to calls to plot; both appear a couple of liens later when their get_visible and set_visible methods are called within a custom function passed in when *on_click* is called.

from matplotlib.widgets import CheckButtons

ax = plt.subplot(111)
l0, = ax.plot(s, t, visible=False, lw=2)
l1, = ax.plot(t, s1, lw=2)

rax = plt.axes( [.05, .4, .1, .15] )
check = CheckButtons(rax, ('raw', 'transformed'), (False, True))

def fnx(checkbox_label):
    if checkbox_label == 'raw': 
        l0.set_visible(not l0.get_visible())
    elif checkbox_label == 'transformed': 
        l1.set_visible(not l1.get_visible())

check.on_clicked(fnx)

plt.show()
like image 388
doug Avatar asked Mar 16 '12 04:03

doug


2 Answers

It's sequence unpacking for a single element.

>>> l = [3]
>>> v, = l
>>> v
3
like image 139
Ignacio Vazquez-Abrams Avatar answered Oct 29 '22 07:10

Ignacio Vazquez-Abrams


l, = v

Is [almost] the same as

[l] = v

Example

>>> l=[3]
>>> [v] = l
>>> v
3
>>>
like image 27
warvariuc Avatar answered Oct 29 '22 08:10

warvariuc