在这段代码中,为什么walrus运算符不将关键字参数figsize传递给matplotlib.pyplot.figure?
#TODO: visualize whether the index is a valid x_value
fontsize=21
plt.figure(figsize:=(8,8))
plt.scatter(x_values_theory, y_values_theory, label='Theory')
plt.scatter(x_values_experimental, y_values_experimental, label='Experiment')
plt.xlabel('xlabel', fontsize=fontsize)
plt.ylabel('ylabel', fontsize=fontsize)
plt.legend(fontsize=fontsize)
plt.tick_params(labelsize=fontsize)
plt.show()收益率
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-55-94183c23eb8f> in <module>
1 #TODO: visualize whether the index == df.[time
2 fontsize=21
----> 3 plt.figure(figsize:=(8,8))
4 plt.scatter(x_values_theory, y_values_theory, label='Theory')
5 plt.scatter(x_values_experimental, y_values_experimental, label='Experiment')
/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, clear, **kwargs)
649 num = allnums[inum]
650 else:
--> 651 num = int(num) # crude validation of num argument
652
653 figManager = _pylab_helpers.Gcf.get_fig_manager(num)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'发布于 2020-08-25 10:38:21
关键字参数使用=而不是:=指定。每个the PEP
:=运算符可以直接在位置函数调用参数中使用;但是,它直接在关键字参数中无效。
所以这意味着
plt.figure(figsize:=(8,8))等同于
figsize = (8,8)
plt.figure(figsize)所以,如果你使用正确的操作符,你的代码应该可以工作:
plt.figure(figsize=(8,8))如果要同时赋值和传递关键字参数,则需要同时编写这两个内容,例如:
plt.figure(figsize=(figsize:=(8,8)))请注意,括号是必需的。
https://stackoverflow.com/questions/63571204
复制相似问题