diff --git a/pyltk/pgfplots/axis.py b/pyltk/pgfplots/axis.py index d9019bf..43216c9 100644 --- a/pyltk/pgfplots/axis.py +++ b/pyltk/pgfplots/axis.py @@ -15,9 +15,9 @@ class axis(element): \\end{{axis}}""" } - def __init__(self, *args, parent=None, legend=True, label=None, **kargs): + def __init__(self, *args, parent=None, legend=True, label=None, **kwargs): super().__init__(parent, label=label) - self.options = kargs + self.options = kwargs self.legend = legend for arg in args: self.add(arg) diff --git a/pyltk/pgfplots/coordinates_plot.py b/pyltk/pgfplots/coordinates_plot.py index 281237a..9c8df92 100644 --- a/pyltk/pgfplots/coordinates_plot.py +++ b/pyltk/pgfplots/coordinates_plot.py @@ -11,5 +11,6 @@ class coordinates_plot(generic_plot): def format_data(self, data): pts = [] for d in data: - pts.append('({}, {})'.format(*d)) + pts.append('({}, {})'.format( + *self.format_values(d))) return 'coordinates {{{}}}'.format(''.join(pts)) diff --git a/pyltk/pgfplots/errorbars_plot.py b/pyltk/pgfplots/errorbars_plot.py index 0af311e..fb86aec 100644 --- a/pyltk/pgfplots/errorbars_plot.py +++ b/pyltk/pgfplots/errorbars_plot.py @@ -5,7 +5,7 @@ from .generic_plot import generic_plot class errorbars_plot(generic_plot): - def __init__(self, legend, data, label=None, options=[]): + def __init__(self, legend, data, precision=5, label=None, options=[]): options = options + [ 'error bars/.cd', ('y dir', 'both'), ('y explicit')] super().__init__(legend, data, label=label, options=options) @@ -20,5 +20,6 @@ class errorbars_plot(generic_plot): p = d[2] n = p rows.append('({x}, {y}) += (0, {p}) -= (0, {n})'.format( - x=x, y=y, p=p, n=n)) + x=self.format_value(x), y=self.format_value(y), + p=self.format_value(p), n=self.format_value(n))) return 'coordinates {{{}}}'.format('\n'.join(rows)) diff --git a/pyltk/pgfplots/generic_plot.py b/pyltk/pgfplots/generic_plot.py index 6b3af16..96112d1 100644 --- a/pyltk/pgfplots/generic_plot.py +++ b/pyltk/pgfplots/generic_plot.py @@ -11,11 +11,12 @@ class generic_plot(element): 'content': """\\addplot{options} {data};""" } - def __init__(self, legend, data, label=None, options=[]): + def __init__(self, legend, data, precision=6, label=None, options=[]): super().__init__(label=label) self.legend = legend self.data = data self.options = options + self.precision = precision def format_options(self, options): if not options: @@ -29,6 +30,12 @@ class generic_plot(element): 'content': ', '.join(opts) }) + def format_value(self, value): + return '{:.{prec}f}'.format(value, prec=self.precision) + + def format_values(self, values): + return [self.format_value(value) for value in values] + def content(self): return self.fmt().format('content', { 'data': self.format_data(self.data),