我想创建一个在调用plt.plot
时自动正常运行的类:
import matplotlib.pyplot as plt
class MyDataClass:
self.data = [...]
self.time = [...]
# stuff
instance = MyDataClass(...)
plt.plot(instance) # plots as if I called: plt.plot(self.time, self.data)
我知道大熊猫这样做,但我不知道怎么做。
答案 0 :(得分:2)
我创建了一个plot
方法
class MyDataClass:
def __init__(self):
self.data = [...]
self.time = [...]
# stuff
def plot(self):
plt.plot(self.time, self.data);
instance = MyDataClass(...)
instance.plot()
Minimal and Complete Verifiable Example (MCVE)
class MyDataClass:
def __init__(self):
self.data = [1, 2, 3]
self.time = [3, 2, 1]
# stuff
def plot(self):
plt.plot(self.time, self.data);
instance = MyDataClass()
instance.plot()
答案 1 :(得分:0)
要准确回答如何向plt.plot()
函数提供类实例的问题,可以采用以下方法:
import matplotlib.pyplot as plt
class MyDataClass():
data = [1,2,4]
time = [1,2,3]
def __call__(self):
return (self.time,self.data)
instance = MyDataClass()
plt.plot(*instance())
plt.show()
如图所示,您无法直接调用plt.plot(instance)
,因为plot
期望其参数为列表或数组。相反,您需要使用对类的调用的返回值。
答案 2 :(得分:0)
我认为更多的实践是这样显示:
enter from System.IO import Path, File, StreamWriter
from Spotfire.Dxp.Application.Visuals import TablePlot
from System.IO import Path, File, StreamWriter
from Spotfire.Dxp.Application.Visuals import TablePlot
from Spotfire.Dxp.Data import DataPropertyClass
from Spotfire.Dxp.Data import DataType
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from Spotfire.Dxp.Data import DataPropertyClass
from Spotfire.Dxp.Data import DataType
from System.IO import File, Directory
from System.IO import Path, File, StreamWriter
from Spotfire.Dxp.Application.Visuals import CrossTablePlot
report1 = "C:\\export\\report1.csv"
writer = StreamWriter(report1)
Viz1.As[CrossTablePlot]().ExportText(writer)
接下来输入数据:
class MyDataClass:
def __init__(self, data, time):
self.data = data
self.time = time
def plot(self):
plt.plot(self.time, self.data)
import matplotlib.pyplot as plt
好吧
data = [1, 2, 3]
time = [3, 2, 1]
kot = MyDataClass(data,time)