3 回答

TA貢獻(xiàn)1869條經(jīng)驗 獲得超4個贊
您可以在函數(shù)簽名中使用argsandkwargs并將參數(shù)傳遞給plot()函數(shù)。有很多很好的解釋來解釋它們是如何工作的,所以我不會在這里重復(fù)。
本質(zhì)上args并kwargs允許您傳遞可變數(shù)量的參數(shù)。在這種情況下,kwargs它會打包您傳遞給字典中函數(shù)的任何“額外”關(guān)鍵字參數(shù)。然后可以將字典傳遞到接收函數(shù)中并使用**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)
使用函數(shù)參數(shù)調(diào)用它并添加您需要的任何額外參數(shù)plot():
plot_measurement("x_label", "y_label", x, y, latex_mode = False, linewidth = 1, label = "plot_label")
args并將kwargs“吸收”您傳遞給函數(shù)的任何額外參數(shù)。要使用您的關(guān)鍵字參數(shù),請將其放在函數(shù)簽名中所有位置參數(shù)之后 - 現(xiàn)在包括*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")
生產(chǎn):

TA貢獻(xiàn)2011條經(jīng)驗 獲得超2個贊
您可以將單個參數(shù)字典傳遞給plot_measurement所有位置參數(shù),將第二個參數(shù)字典傳遞給所有可選參數(shù),以使事情更簡單。傳統(tǒng)上,這些被稱為args和kwargs(關(guān)鍵字參數(shù))。使用 a *as in*args展開一個列表并將每個列表元素作為參數(shù)放入函數(shù)中;類似地,**展開一個字典并將每個字典鍵值對放入函數(shù)中(這對于關(guān)鍵字參數(shù)很方便)
# 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貢獻(xiàn)1827條經(jīng)驗 獲得超4個贊
對于后代,此響應(yīng)中發(fā)布的代碼不起作用,并且是響應(yīng)@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)
使用以下咒語調(diào)用。
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()
添加回答
舉報