3 回答
TA贡献1869条经验 获得超4个赞
您可以在函数签名中使用argsandkwargs并将参数传递给plot()函数。有很多很好的解释来解释它们是如何工作的,所以我不会在这里重复。
本质上args并kwargs允许您传递可变数量的参数。在这种情况下,kwargs它会打包您传递给字典中函数的任何“额外”关键字参数。然后可以将字典传递到接收函数中并使用**kwargs
对于您的功能:
def plot_measurement(x_label, y_label, *args, latex_mode = False, **kwargs):
# Keyword arguments can be accessed as a normal dictionary
if (kwargs["label"] == ""):
kwargs["label"] = self.identifier
if (latex_mode):
matplotlib.rc("text", usetex = True)
matplotlib.rc("font", family = "serif")
matplotlib.pyplot.plot(*args, **kwargs)
matplotlib.pyplot.xlabel(x_label)
matplotlib.pyplot.ylabel(y_label)
使用函数参数调用它并添加您需要的任何额外参数plot():
plot_measurement("x_label", "y_label", x, y, latex_mode = False, linewidth = 1, label = "plot_label")
args并将kwargs“吸收”您传递给函数的任何额外参数。要使用您的关键字参数,请将其放在函数签名中所有位置参数之后 - 现在包括*args.
完整的工作示例:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def plot_measurement(x_label, y_label, *args, latex_mode = False, **kwargs):
if (kwargs["label"] == ""):
kwargs["label"] = self.identifier
if (latex_mode):
matplotlib.rc("text", usetex = True)
matplotlib.rc("font", family = "serif")
plt.plot(*args, **kwargs)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.show()
x = np.arange(0, 20)
x = np.reshape(x, (4, 5))
y = np.arange(5, 25)
y = np.reshape(y, (4, 5))
plot_measurement("x axis label", "y axis label", x, y, latex_mode = False, color = "red", label = "plot label")
生产:
TA贡献2011条经验 获得超2个赞
您可以将单个参数字典传递给plot_measurement所有位置参数,将第二个参数字典传递给所有可选参数,以使事情更简单。传统上,这些被称为args和kwargs(关键字参数)。使用 a *as in*args展开一个列表并将每个列表元素作为参数放入函数中;类似地,**展开一个字典并将每个字典键值对放入函数中(这对于关键字参数很方便)
# also this is standard because it's very convenient
import matplotlib.pyplot as plt
## Examples of how args and kwargs are formatted
# all required arguments go in a list in order
args = [x,y,format]
# all non-required (keyword) arguments go in a dictionary
kwargs = {
line_width: 1,
label: plot_label
}
def plot_measurement(self,args,kwargs,plot_label,x_label,y_label,latex_mode = False):
# here all of the args and keyword args are passed together
# whereas all arguments used directly by plot_measurement are not passed together
# though they could be for cleanliness
if (plot_label == ""):
plot_label = self.identifier
if (latex_mode):
matplotlib.rc("text", usetex = True)
matplotlib.rc("font", family = "serif")
plt.plot(*args, **kwargs)
plt.xlabel(x_label)
plt.ylabel(y_label)
TA贡献1827条经验 获得超4个赞
对于后代,此响应中发布的代码不起作用,并且是响应@Derek 和@Erik 的一个小测试用例。
我看不到如何在评论中放置格式化代码,所以我把它贴在这里。请原谅我的罪过!
def plot_measurement(self, latex_mode = False, *args, **kwargs):
print("\nlen(args) = {0}, args = {1}".format(len(args), args))
print("\nlen(kwargs) = {0}, kwargs = {1}\n".format(len(kwargs), kwargs))
if (latex_mode):
matplotlib.rc("text", usetex = True)
matplotlib.rc("font", family = "serif")
matplotlib.pyplot.plot(*args, **kwargs)
使用以下咒语调用。
test_measurement1.plot_measurement(test_measurement1.data[6], test_measurement1.data[15])
data[6] 和 data[15] 都是 numpy.arrays 并连接在一起。输出如下:
len(args) = 1, args = (array([-8.21022986e-06, -8.19599736e-06, -8.16865495e-06, ...,
-7.70015886e-06, -7.70425522e-06, -7.71744717e-06]),)
len(kwargs) = 0, kwargs = {}
还有,代码错误就行了
if (latex_mode):
给出错误
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
添加回答
举报