Matplotlib 中经常使用的一个不寻常的 Python 语法元素 [英] An unusual Python syntax element frequently used in Matplotlib
问题描述
一个附带条件:我问题核心的语法元素是 Python 语言;但是,该元素经常出现在Matplotlib库中,这是我所见过的唯一上下文.因此,我不确定是一般的Python语法问题还是库特定的问题.我所知道的是,无论是在 Python语言参考还是在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.
那些使用和/或使用优秀的 Python 绘图库开发的人,Matplotlib 将识别下面的语法模式.(
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?
我熟悉Python的赋值解包,例如
I am familiar with Python's assignment unpacking, e.g.,
>>> a, b = [100, 200]
我也知道在 Python 中单项 元组有时表示为 t,
I'm also aware that in Python one-item tuples are sometimes represented as t,
可能是上述第一个问题的答案;如果是这样,那么我还不明白为什么这里只需要从 plot 调用返回的值的第一个元素.
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.
(注意:"l"是小写的"ell";我使用此字母是因为ls是此处最常用的字母,可能是因为它绑定到以相同字母开头的对象,请参见下文).
(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).
一些额外的上下文:
对 plot 的调用返回 line2D 实例列表:
The call to plot returns a list of line2D instances:
>>> type(l)
<class 'matplotlib.lines.Line2D'>
所以l是line2D类型的对象.
绑定到lines2D对象后,通常会在Matplotlib代码中看到此变量",如下所示:
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")
下面是另一个示例;它显示了这种变量逗号"结构的常见场景,即在 Matplotlib 绘图窗口中嵌入小型工具包/图形后端独立小部件,例如,通过复选框打开/关闭,绘图窗口中出现多个数据系列
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.
在下面的代码中,创建了一个简单的 Matplotlib 图和一个简单的小部件,该小部件由两个复选框组成,每个复选框对应一个数据系列.
In the code below, a simple Matplotlib plot and a simple widget comprised of two checkboxes one for each data series are created.
l0 和 l1 再次绑定到对 plot 的调用;当在调用 *on_click* 时传入的自定义函数中调用 get_visible 和 set_visible 方法时,两者都会出现几个留置权.
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()
推荐答案
l, = v
[几乎]与
[l] = v
示例
>>> l=[3]
>>> [v] = l
>>> v
3
>>>
这篇关于Matplotlib 中经常使用的一个不寻常的 Python 语法元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!