def plot(self, filename):
r"""Save an image file of the transfer function.
This function loads up matplotlib, plots all of the constituent
transfer functions and saves.
Parameters
----------
filename : string
The file to save out the plot as.
Examples
--------
>>> tf = ColorTransferFunction( (-10.0, -5.0) )
>>> tf.add_layers(8)
>>> tf.plot("sample.png")
"""
from matplotlib import pyplot
from matplotlib.ticker import FuncFormatter
pyplot.clf()
ax = pyplot.axes()
i_data = np.zeros((self.alpha.x.size, self.funcs[0].y.size, 3))
i_data[:,:,0] = np.outer(np.ones(self.alpha.x.size), self.funcs[0].y)
i_data[:,:,1] = np.outer(np.ones(self.alpha.x.size), self.funcs[1].y)
i_data[:,:,2] = np.outer(np.ones(self.alpha.x.size), self.funcs[2].y)
ax.imshow(i_data, origin='lower')
ax.fill_between(np.arange(self.alpha.y.size), self.alpha.x.size * self.alpha.y, y2=self.alpha.x.size, color='white')
ax.set_xlim(0, self.alpha.x.size)
xticks = np.arange(np.ceil(self.alpha.x[0]), np.floor(self.alpha.x[-1]) + 1, 1) - self.alpha.x[0]
xticks *= (self.alpha.x.size-1) / (self.alpha.x[-1] - self.alpha.x[0])
ax.xaxis.set_ticks(xticks)
def x_format(x, pos):
return "%.1f" % (x * (self.alpha.x[-1] - self.alpha.x[0]) / (self.alpha.x.size-1) + self.alpha.x[0])
ax.xaxis.set_major_formatter(FuncFormatter(x_format))
yticks = np.linspace(0,1,5) * self.alpha.y.size
ax.yaxis.set_ticks(yticks)
def y_format(y, pos):
return (y / self.alpha.y.size)
ax.yaxis.set_major_formatter(FuncFormatter(y_format))
ax.set_ylabel("Transmission")
ax.set_xlabel("Value")
pyplot.savefig(filename)
评论列表
文章目录