Matplotlib:创建一个可以绘制的类

时间:2017-04-25 20:49:57

标签: python python-3.x pandas matplotlib

我想创建一个在调用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)

我知道大熊猫这样做,但我不知道怎么做。

3 个答案:

答案 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()

enter image description here

答案 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)

enter image description here