我想爲我的科學數據創建自定義繪圖的模塊,但我不知道如何將(*arg2,**kwarg2)
類型的參數傳遞給方法。如何將(* args,** kwargs)類型的屬性傳遞給類中的函數?
下面是相關代碼:
import numpy as np
import matplotlib.pyplot as plt
# class of figures for channel flow
# with one subfigure
class Homfig:
# *args: list of plot features for ax1.plot
# (xdata,ydata,str linetype,str label)
# **kwargs: list of axes features for ax1.set_$(STH)
# possible keys:
# title,xlabel,ylabel,xlim,ylim,xscale,yscale
def __init__(self,*args,**kwargs):
self.args = args
self.kwargs = kwargs
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
for key, val in self.kwargs.iteritems():
getattr(self.ax,'set_'+key)(val)
def hdraw(self):
for arg in self.args:
self.ax.plot(*arg)
leg = self.ax.legend(loc=4)
的問題是,arg
本身就像(*agr2,**kwarg2)
一個元組,當我打電話 self.ax.plot(* ARG) 它不會看到一個名爲變量。 功能hdraw
應該從init獲得的輸入*args
上運行,繪圖行數據通過arg
。 我該如何表達arg
由未命名變量和已命名變量組成? self.ax.plot
可稱爲多於一次,當我想有幾行
我打電話從其他python腳本的模塊(具有不同label
,linetype
等,在一個情節。):
meanfig = hfig.Homfig((datax,datay),title='test plot')
meanfig.hdraw()
如何添加像標籤或線型的self.ax.plot功能,例如:
meanfig = hfig.Homfig((datax,datay,label="test_label",linetype ='o'),title='test plot')
可能重複[將所有參數傳遞給另一個函數](https://stackoverflow.com/questions/42499656/pass-all-arguments-of-a-function-to-another-function) –