replot/replot/__init__.py

50 lines
1.7 KiB
Python
Raw Permalink Normal View History

2016-03-01 14:51:25 +01:00
"""
2016-03-18 20:45:40 +01:00
The :mod:`replot` module is an attempt at an easier API to plot graphs using
Matplotlib.
2016-03-01 14:51:25 +01:00
"""
2016-03-18 20:45:40 +01:00
from replot.figure import Figure
2016-03-18 20:45:40 +01:00
__all__ = ["plot", "Figure"]
def plot(data, **kwargs):
"""
2016-03-02 14:04:28 +01:00
Helper function to make one-liner plots. Typical use case is:
>>> replot.plot([range(10),
(np.sin, (-5, 5)),
(lambda x: np.sin(x) + 4, (-10, 10), {"linewidth": 10}),
2016-03-02 16:14:50 +01:00
(lambda x: np.sin(x) - 4, (-10, 10), {"linewidth": 10}),
2016-03-02 14:04:28 +01:00
([-i for i in range(5)], {"linewidth": 10})],
xlabel="some x label",
ylabel="some y label",
title="A title for the figure",
legend="best",
2016-03-08 17:12:02 +01:00
palette=seaborn.color_palette("husl", 2))
"""
# Init new figure
figure = Figure(**kwargs)
2016-03-02 14:04:28 +01:00
# data is a list of plotting commands
for plot_ in data:
# If we provide a tuple, handle it
2016-03-02 14:04:28 +01:00
if isinstance(plot_, tuple):
args = ()
kwargs = {}
# First case, only two items provided
2016-03-02 14:04:28 +01:00
if len(plot_) == 2:
# Parse args and kwargs according to type of items
2016-03-02 14:04:28 +01:00
if isinstance(plot_[1], tuple):
args = (plot_[1],)
elif isinstance(plot_[1], dict):
kwargs = plot_[1]
# Second case, at least 3 items provided
2016-03-02 14:04:28 +01:00
elif len(plot_) > 2:
# Then, args and kwargs are well defined
2016-03-02 14:04:28 +01:00
args = (plot_[1],)
kwargs = plot_[2]
# Pass the correct argument to plot function
2016-03-02 14:04:28 +01:00
figure.plot(plot_[0], *args, **kwargs)
else:
2016-03-02 14:04:28 +01:00
figure.plot(plot_)
figure.show()