I am trying to write a simple Python matplotlib plotting Class that will set up formatting of the plot as I would like. To do this, I am using a class and a subclass. The parent generates an axis and figure, then sets all axis properties that I would like. Then the child
generates the plot itself - scatter plot, line, etc. - and saves the plot to an output file.
Here is what I have come up with for the purposes of this question (the plot itself is based on this code):
import matplotlib.pyplot as plt
import numpy as np
class myplot():
def __init__(self,dims,padd):
self.fig = plt.figure(figsize=(8.5, 11))
self.ax = self.fig.add_axes(dims)
self.pad = padd
def ax_prop(self):
self.ax.set_axisbelow(True)
self.ax.tick_params(axis='x', pad=self.pad, which='both')
class plotter(myplot):
def __init__(self,dims,padd,x,y):
myplot.__init__(self,dims,padd)
self.x = x
self.y = y
def mk_p(self):
self.ax.plot(self.x, self.y, linestyle = '-')
plt.savefig('Outfile.png', facecolor=self.fig.get_facecolor(), dpi = 300)
plt.show()
if __name__ == "__main__":
x = np.arange(0.0, 2.0, 0.01)
y = np.sin(2*np.pi*x)
propr = [0.60, 0.2, 0.2, 0.25]; padding =5
plt_instance = myplot(propr,padding)
plt_instance.ax_prop()
pl_d = plotter(propr,padding,x,y)
pl_d.mk_p()
I'm tying to base this on the following:
The child must have all the properties of the parent.
Therefore, the child
class plotter()
must inherit the:
ax, and all its customized properties
fig
from the parent (myplot()
) class. The following may change in the Child
class:
- type of plot - scatter, line, etc.
- any properties of the type of plot -
markersize
,linestyle
, etc.
but the matplotlib
figure and axis objects (fig
,ax
) will always be required before plotting an input set of numbers.
Question:
Is this a correct usage of Python inheritance or is the child class superfluous in this case? If so, was there a place where the reasoning for a subclass was not consistent with object oriented programming (I'm having difficulty convincing myself of this)?