Add logplot and loglogplot methods. Closes https://github.com/Phyks/replot/issues/14

This commit is contained in:
Lucas Verney 2016-03-09 11:46:35 +01:00
parent d672b2873e
commit 09ea785806
3 changed files with 1662 additions and 33 deletions

File diff suppressed because one or more lines are too long

View File

@ -74,6 +74,11 @@ design in the API, or required feature!
compare them easily, but you do not want to waste time setting up a grid
and placing your plots at the correct place? <code>replot</code> handles
it for you out of the box!</dd>
<dt>Easy plotting in log scale</dt>
<dd><code>replot</code> defines <code>logplot</code> and
<code>loglogplot</code> shortcuts functions to plot in <em>log</em> scale
or <em>loglog</em> scale.</dd>
</dl>

View File

@ -329,6 +329,9 @@ class Figure():
pass
plot_ = (args, kwargs)
# Keep track of the custom kwargs
plot_ += (custom_kwargs,)
# Add the plot to the correct group
if "group" in custom_kwargs:
self.plots[custom_kwargs["group"]].append(plot_)
@ -340,6 +343,28 @@ class Figure():
if "label" in kwargs and self.legend is None:
self.legend = True
def logplot(self, *args, **kwargs):
"""
Plot something on the :class:`Figure` object, in log scale.
.. note:: See :func:`replot.Figure.plot` for the full documentation.
>>> with replot.figure() as fig: fig.logplot(np.log, (-1, 1))
"""
kwargs["logscale"] = "log"
self.plot(*args, **kwargs)
def loglogplot(self, *args, **kwargs):
"""
Plot something on the :class:`Figure` object, in log-log scale.
.. note:: See :func:`replot.Figure.plot` for the full documentation.
>>> with replot.figure() as fig: fig.logplot(np.log, (-1, 1))
"""
kwargs["logscale"] = "loglog"
self.plot(*args, **kwargs)
def _legend(self, axis, overload_legend=None):
"""
Helper function to handle ``legend`` attribute. It places the legend \
@ -502,6 +527,13 @@ class Figure():
# Plot
for plot_ in self.plots[group_]:
tmp_plots = axis.plot(*(plot_[0]), **(plot_[1]))
# Handle custom kwargs at plotting time
if "logscale" in plot_[2]:
if plot_[2]["logscale"] == "log":
axis.set_xscale("log")
elif plot_[2]["logscale"] == "loglog":
axis.set_xscale("log")
axis.set_yscale("log")
# Do not clip line at the axes boundaries to prevent
# extremas from being cropped.
for tmp_plot in tmp_plots:
@ -704,10 +736,14 @@ def _handle_custom_plot_arguments(kwargs):
del kwargs["group"]
# Handle "line" argument
if "line" in kwargs:
if not kwargs["line"]:
if not kwargs["line"]: # If should not draw lines, set kwargs for it
kwargs["linestyle"] = "None"
kwargs["marker"] = "x"
del kwargs["line"]
# Handle "logscale" argument
if "logscale" in kwargs:
custom_kwargs["logscale"] = kwargs["logscale"]
del kwargs["logscale"]
return (kwargs, custom_kwargs)