An unusual Python syntax element frequently used in Matplotlib

2024/11/15 11:18:28

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 CheckButtonsax = 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()
Answer

It's sequence unpacking for a single element.

>>> l = [3]
>>> v, = l
>>> v
3
https://en.xdnf.cn/q/71470.html

Related Q&A

Control the power of a usb port in Python

I was wondering if it could be possible to control the power of usb ports in Python, using vendor ids and product ids. It should be controlling powers instead of just enabling and disabling the ports. …

Threads and local proxy in Werkzeug. Usage

At first I want to make sure that I understand assignment of the feature correct. The local proxy functionality assigned to share a variables (objects) through modules (packages) within a thread. Am I …

Unable to use google-cloud in a GAE app

The following line in my Google App Engine app (webapp.py) fails to import the Google Cloud library:from google.cloud import storageWith the following error:ImportError: No module named google.cloud.st…

Multiple thermocouples on raspberry pi

I am pretty new to the GPIO part of the raspberry Pi. When I need pins I normally just use Arduino. However I would really like this project to be consolidated to one platform if possible, I would li…

Strange behaviour when mixing abstractmethod, classmethod and property decorators

Ive been trying to see whether one can create an abstract class property by mixing the three decorators (in Python 3.9.6, if that matters), and I noticed some strange behaviour. Consider the following …

Center the third subplot in the middle of second row python

I have a figure consisting of 3 subplots. I would like to locate the last subplot in the middle of the second row. Currently it is located in the left bottom of the figure. How do I do this? I cannot …

Removing columns which has only nan values from a NumPy array

I have a NumPy matrix like the one below:[[182 93 107 ..., nan nan -1][182 93 107 ..., nan nan -1][182 93 110 ..., nan nan -1]..., [188 95 112 ..., nan nan -1][188 97 115 ..., nan nan -1][188 95 112 ..…

how to get kubectl configuration from azure aks with python?

I create a k8s deployment script with python, and to get the configuration from kubectl, I use the python command:from kubernetes import client, configconfig.load_kube_config()to get the azure aks conf…

Object vs. Dictionary: how to organise a data tree?

I am programming some kind of simulation with its data organised in a tree. The main object is World which holds a bunch of methods and a list of City objects. Each City object in turn has a bunch of m…

Fastest way to compute distance beetween each points in python

In my project I need to compute euclidian distance beetween each points stored in an array. The entry array is a 2D numpy array with 3 columns which are the coordinates(x,y,z) and each rows define a ne…