6
votes

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()
3
To give a bit of a reason as to why plot returns a sequence, plot is modeled off of matlab's plot, so it accepts an arbitrary number of lines to plot with a single function call. (e.g. you can do things like plt.plot(x, 1*x, x, 2*x, x, 3*x, x, 4*x) and plot 4 lines, or pass in 2d arrays and get a line for each row) Because of this, plot always returns a sequence, even if there's only one line. It's actually fairly rare to plot more than one line at a time with plot, so you very often see l, = plt.plot(x, y). - Joe Kington
frequently used in Matplotlib could you link to an example in the docs? - n611x007

3 Answers

17
votes

It's sequence unpacking for a single element.

>>> l = [3]
>>> v, = l
>>> v
3
2
votes
l, = v

Is [almost] the same as

[l] = v

Example

>>> l=[3]
>>> [v] = l
>>> v
3
>>>
0
votes

Some explanations:

we know

a, b = 1, 2

gives a == 1, b == 2

in my opinion, if python found the left side is a "tuple", it would unpack the right side.

a, = [1]

python may think it as (a,) = [1]

then try to unpack it, so we get a == 1