在matplotlib中传递x和y数据作为关键字参数? [英] passing x- and y-data as keyword arguments in matplotlib?

查看:482
本文介绍了在matplotlib中传递x和y数据作为关键字参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者,为什么不

import numpy
import matplotlib.pyplot as plt
plt.plot(xdata = numpy.array([1]), ydata = numpy.array(1), color = 'red', marker = 'o')

工作? c.f。

> In [21]: import numpy

> In [22]: import matplotlib.pyplot as plt

> In [23]: plt.plot(xdata = numpy.array([1]), ydata = numpy.array(1), color = 'red', marker = 'o')
> Out[23]: []

> In [24]: plt.plot([1],[1], color = 'red', marker = 'o')
> Out[24]: [<matplotlib.lines.Line2D at 0x108036890>]

> In [25]: plt.plot(1, 1, color = 'red', marker = 'o')
> Out[25]: [<matplotlib.lines.Line2D at 0x1041024d0>]


推荐答案

只是为了扩展@Yann已经说过的内容:

Just to expand on what @Yann already said:

要理解为什么会发生这种情况,你需要更多地了解matplotlib的结构。为了允许像code> plt.setp 这样的matlab-isms,为了保持与旧版本python的兼容性,matplotlib避免了属性,并且在很大程度上依赖于getter和setter。 ( plot 实际上是最复杂的情​​况之一,仅仅是由于它支持的所有疯狂形式的调用。)

To understand why this happens, you need to understand a bit more about matplotlib's structure. To allow "matlab-isms" like plt.setp, and to maintain compatibility with older versions of python, matplotlib avoid properties and relies heavily on getters and setters. (plot is actually one of the most complex cases, simply due to all of the crazy forms of calling it supports.)

你可以提出一个很好的论据,即这是一个过时的,非语音设计,但这不是重点。

You can make a good argument that this is an outdated, unpythonic design, but that's beside the point.

实际发生的事情(对于最简单的<$的情况) c $ c> plot(x,y,other = stuff))当你调用 plot 是一个新的 matplotlib .line.Line2D 对象是从前两个参数创建的,然后调用 matplotlib.line.Line2D.update(kwargs)

What actually happens (for the simplest case of plot(x, y, other=stuff)) when you call plot is that a new matplotlib.line.Line2D object is created from the first two arguments, and then matplotlib.line.Line2D.update(kwargs) is called.

更新基本上可以:

for key, value in kwargs.iteritems():
    func = getattr(self, 'set_'+key)
    func(value)

我过度简化了,但这是基本的想法。

I'm over-simplifying, but that's the basic idea.

也是接受的关键字参数列表基本上是从具有 set _ * 的任何内容自动生成的。因为 Line2D set_xdata set_ydata 方法,它们会出现在关键字参数列表中。

Also the accepted keyword argument list is basically auto-generated from anything that has a set_*. Because Line2D has set_xdata and set_ydata methods, they show up in the keyword argument list.

关键是,在 Line2D ,如果你没有指定任何参数, plot 将不会初始化任何 Line2D 's 。

The point is, that the keyword arguments are never actually used until after most of the initialization of Line2D, and if you don't specify any arguments, plot won't initialize any Line2D's.

您可以认为这是一个错误,但我怀疑它是否会被修复。我不认为 xdata ydata 曾被用作关键字参数。

You could consider this a bug, but I doubt it would be fixed. I don't think xdata and ydata were ever intended to be used as keyword arguments.

set_xdata set_ydata 是否允许你快速更新 Line2D 实例,而不是创建新实例(对于动画等等)。由于matplotlib的设置方式,它们恰好被允许作为关键字参数。

set_xdata and set_ydata are there to allow you to quickly update a Line2D instance instead of creating a new one (For animations, etc...). They just happen to be allowed as keyword arguments due to the way matplotlib is set up.

这篇关于在matplotlib中传递x和y数据作为关键字参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆