From: <mh...@us...> - 2011-11-19 14:01:51
|
Revision: 318 http://gnuplot-py.svn.sourceforge.net/gnuplot-py/?rev=318&view=rev Author: mhagger Date: 2011-11-19 14:01:45 +0000 (Sat, 19 Nov 2011) Log Message: ----------- Add method Gnuplot.set_tics(). Allow axis tic marks and labels to be set a la "set xtics" etc. Suggested by: Lorenzo Mentaschi <lor...@ya...> Modified Paths: -------------- trunk/_Gnuplot.py trunk/__init__.py trunk/test.py Modified: trunk/_Gnuplot.py =================================================================== --- trunk/_Gnuplot.py 2011-11-19 07:59:32 UTC (rev 317) +++ trunk/_Gnuplot.py 2011-11-19 14:01:45 UTC (rev 318) @@ -57,6 +57,35 @@ self.flush() +class Tic: + """An explicit ticmark definition. + + An instance of this class defines a tic mark explicitly, a la + gnuplot's 'set xtics (...)' command.""" + + def __init__(self, position, label=None, level=None): + self.position = position + if label is None: + if level is not None: + label = str(position) + elif not isinstance(label, basestring): + raise TypeError('label must be a string') + self.label = label + if level is None: + self.level = None + else: + self.level = int(level) + + def __str__(self): + retval = [] + if self.label is not None: + retval.append('"%s"' % (self.label,)) + retval.append(str(self.position)) + if self.level is not None: + retval.append(str(self.level)) + return ' '.join(retval) + + class Gnuplot: """Interface to a gnuplot program. @@ -472,6 +501,39 @@ self.set_label('title', s, offset=offset, font=font) + def set_tics(self, axis, value): + """Configure the tics for the given axis. + + axis may be 'x', 'y', 'z', 'x2', or 'y2'. + + value can be a string, in which case it is passed to gnuplot's + 'set xtics', 'set ytics', etc. command. Or it can be a list + of Tic objects or tuples specifying where each individual + tic/label should be placed. Any list entries that are tuples + are passed to the Tic constructor and therefore must be in one + of the following forms: + + (pos,) + (pos, label) + (pos, label, level) + + where pos is the position of the tic mark (as a number), label + is a string that will be used to label the tic, and level is 0 + (for major tics) or 1 (for minor tics). + + """ + + if type(value) is types.StringType: + tics_string = value + else: + tics_strings = [] + for tic in value: + if not isinstance(tic, Tic): + tic = Tic(*tic) + tics_strings.append(str(tic)) + tics_string = '(%s)' % (', '.join(tics_strings),) + self('set %stics %s' % (axis, tics_string,)) + def hardcopy(self, filename=None, terminal='postscript', **keyw): """Create a hardcopy of the current plot. Modified: trunk/__init__.py =================================================================== --- trunk/__init__.py 2011-11-19 07:59:32 UTC (rev 317) +++ trunk/__init__.py 2011-11-19 14:01:45 UTC (rev 318) @@ -163,6 +163,6 @@ from gp import GnuplotOpts, GnuplotProcess, test_persist from Errors import Error, OptionError, DataError from PlotItems import PlotItem, Func, File, Data, GridData -from _Gnuplot import Gnuplot +from _Gnuplot import Gnuplot, Tic Modified: trunk/test.py =================================================================== --- trunk/test.py 2011-11-19 07:59:32 UTC (rev 317) +++ trunk/test.py 2011-11-19 14:01:45 UTC (rev 318) @@ -80,6 +80,21 @@ g.plot(Gnuplot.Func('sin(x)', title='Sine of x')) wait('axes=x2y2') g.plot(Gnuplot.Func('sin(x)', axes='x2y2', title='Sine of x')) + wait('Custom tics') + g.set_tics( + 'x', + [ + (-2 * math.pi, '-2pi'), + (-math.pi, '', 2), + (0,), + Gnuplot.Tic(math.pi, level=2), + Gnuplot.Tic(2 * math.pi, '2pi'), + ], + ) + g.plot(Gnuplot.Func('sin(x)', title='Sine of x')) + wait('Reset to default tics') + g.set_tics('x', 'auto') + g.plot(Gnuplot.Func('sin(x)', title='Sine of x')) print 'Change Func attributes after construction:' f = Gnuplot.Func('sin(x)') This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |