我有一个有很多绘图功能的课程。我的目的是使用matplotlib的subplot方法将所有绘图分组到一个图像中,具体取决于我调用的函数数量。
我尝试了类似下面的内容(这是我的程序的简短版本),但我不知道为什么不起作用。
感谢任何帮助。提前谢谢。
import itertools
import numpy as np
from matplotlib import pyplot as plt
class Base(object):
def __init__(self, a, multiPlot=True, numColGraph=None, numRowGraph=None,
figSize=None, DPI=None, num=None):
self.a = a
self.x = np.linspace(0, 5)
if multiPlot:
self.nCG = numColGraph
self.nRG = numRowGraph
else:
self.nCG = 1
self.nRG = 1
if figSize and DPI:
self.thePlot = plt.figure(figsize=figSize, dpi=DPI)
if num == 0:
self.plotId = itertools.count(1)
def createPlot1(self):
y = self.x**(a/2)
self.thePlot.add_subplot(self.nRG, self.nCG, next(self.plotId))
plt.plot(self.x, y, label=str(self.a)+'/2')
def createPlot2(self):
y = self.x**a
self.thePlot.add_subplot(self.nRG, self.nCG, next(self.plotId))
plt.plot(self.x, y, label=self.a)
def createPlot3(self):
y = self.x**(2*a)
self.thePlot.add_subplot(self.nRG, self.nCG, next(self.plotId))
plt.plot(self.x, y, label=str(self.a)+'*2')
if __name__ == "__main__":
A = np.linspace(0, 2, 5)
for i, a in enumerate(A):
Instance = Base(a, numColGraph=3, numRowGraph=len(A),
figSize=(12,10), DPI=100, num=i)
Instance.createPlot1()
Instance.createPlot2()
Instance.createPlot3()
plt.show()
答案 0 :(得分:1)
至少,您有未定义的变量。我看到这种模式(或类似的东西)三次:
y = self.x**(2*a)
但是在这些案例中你都没有定义a
。也许你的意思是:
y = self.x**(2*self.a)