I want to create a separate class in Python (v2.7.12) that will be saving plots to files (here 'test.eps') and use some labels, titles etc. given by the user as arguments (*args and **kwargs).
The background story is that I have to create plots for scientific data an I want Python class to be simple and fit to my case.
import matplotlib.pyplot as plt
# class of figures for channel flow
# with one subfigure
class Homfig(plt.Figure):
# *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.arg_lst = []
for key,value in kwargs.iteritems():
setattr(self,key,value)
self.arg_lst.append(key)
i=0
for value in args:
setattr(self,'plot'+str(i),value)
i=i+1
print 'list of attributes set: ', self.__dict__.keys()
def hdraw(self):
fig2=plt.figure()
ax1 = fig2.add_subplot(111)
for key in self.arg_lst:
setattr(ax1,'set_'+key,getattr(self,key))
print getattr(ax1,'set_'+key)
ax1.plot([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], label="Test", color='g')
leg = ax1.legend(loc=4)
plt.savefig('test.eps')
plt.clf()
ff = Homfig(title='very important title',xlabel="xxx",ylabel="yyy")
ff.hdraw()
Method "hdraw" should be plotting my data and saving it to a file. The code does not give error message, but no change in plot title or axes labels is seen. Attributes are given to ax1 and the plot is drawn.
Can you help me understand this issue and find a solution?
I run the script in console: python homfigs.py
and the output is: list of attributes set: ['title', 'ylabel', 'xlabel', 'arg_lst'] yyy xxx very important title