# Path: artist/plot.py
# class Plot(SubPlot, BasePlotContainer):
# 
#     """Create a plot containing a single subplot.
# 
#     This class creates a 2D plot.  Its various methods add data,
#     annotations and options which is stored in class variables. Finally,
#     the plot can be rendered using the Jinja2 templating engine
#     resulting in a LaTeX or PDF file.
# 
#     """
# 
#     def __init__(self, axis='', width=r'.67\linewidth', height=None,
#                  font_options=None):
#         environment = jinja2.Environment(loader=jinja2.PackageLoader(
#             'artist', 'templates'), finalize=self._convert_none)
#         self.template = environment.get_template('plot.tex')
# 
#         self.width = width
#         self.height = height
#         self.xmode, self.ymode = self._get_axis_modes(axis)
#         self.font_options = font_options
#         self.external_filename = None
# 
#         super(Plot, self).__init__()
# 
#     def render(self, template=None):
#         """Render the plot using a template.
# 
#         Once the plot is complete, it needs to be rendered.  Artist uses
#         the Jinja2 templating engine.  The default template results in a
#         LaTeX file which can be included in your document.
# 
#         :param template: a user-supplied template or None.
#         :type template: string or None.
#         :returns: the rendered template as string.
# 
#         """
#         if not template:
#             template = self.template
# 
#         self._prepare_data()
# 
#         response = template.render(
#             axis_background=self.axis_background,
#             xmode=self.xmode,
#             ymode=self.ymode,
#             title=self.title,
#             width=self.width,
#             height=self.height,
#             xlabel=self.xlabel,
#             ylabel=self.ylabel,
#             limits=self.limits,
#             xscale=self.xscale,
#             yscale=self.yscale,
#             ticks=self.ticks,
#             axis_equal=self.axis_equal,
#             scalebar=self.scalebar,
#             colorbar=self.colorbar,
#             colormap=self.colormap,
#             external_filename=self.external_filename,
#             font_options=self.font_options,
#             axis_options=self.axis_options,
#             has_graph_paper=self.has_graph_paper,
#             plot=self,
#             plot_template=self.template)
#         return response


import math
import numpy as np
from artist import Plot


def add_custom_pin(plot, x, y, label, distance='3ex'):
    """Add a pin at the maximum value of a data series (sort of)"""

    # only use the second quarter, for cosmetic reasons
    q = int(0.25 * len(y))
    y = y[q:2*q]
    max_index = q + np.argmax(y)
    max_x = x[max_index]
    plot.add_pin(label, x=max_x, location='above', use_arrow=True,
                 style='pin distance=%s' % distance)


X = np.linspace(0., 2 * L, num=1000)
Y_sqr = [square(x) for x in X]
Y = lambda n: [fourier(x, n) for x in X]

graph = Plot()
graph.set_title("Fourier approximation")

graph.plot(x=X, y=Y(3), linestyle='red', mark=None, legend='n=3')
graph.plot(x=X, y=Y(5), linestyle='yellow', mark=None, legend='n=5')
graph.plot(x=X, y=Y(8), linestyle='green', mark=None, legend='n=8')
graph.plot(x=X, y=Y(13), linestyle='blue', mark=None, legend='n=13')
graph.plot(x=X, y=Y(55), linestyle='cyan', mark=None, legend='n=55')
graph.plot(x=X, y=Y_sqr, linestyle='black', mark=None, legend='square')

graph.save('fourier_with_legend')


# Path: artist/plot.py
# class Plot(SubPlot, BasePlotContainer):
# 
#     """Create a plot containing a single subplot.
# 
#     This class creates a 2D plot.  Its various methods add data,
#     annotations and options which is stored in class variables. Finally,
#     the plot can be rendered using the Jinja2 templating engine
#     resulting in a LaTeX or PDF file.
# 
#     """
# 
#     def __init__(self, axis='', width=r'.67\linewidth', height=None,
#                  font_options=None):
#         environment = jinja2.Environment(loader=jinja2.PackageLoader(
#             'artist', 'templates'), finalize=self._convert_none)
#         self.template = environment.get_template('plot.tex')
# 
#         self.width = width
#         self.height = height
#         self.xmode, self.ymode = self._get_axis_modes(axis)
#         self.font_options = font_options
#         self.external_filename = None
# 
#         super(Plot, self).__init__()
# 
#     def render(self, template=None):
#         """Render the plot using a template.
# 
#         Once the plot is complete, it needs to be rendered.  Artist uses
#         the Jinja2 templating engine.  The default template results in a
#         LaTeX file which can be included in your document.
# 
#         :param template: a user-supplied template or None.
#         :type template: string or None.
#         :returns: the rendered template as string.
# 
#         """
#         if not template:
#             template = self.template
# 
#         self._prepare_data()
# 
#         response = template.render(
#             axis_background=self.axis_background,
#             xmode=self.xmode,
#             ymode=self.ymode,
#             title=self.title,
#             width=self.width,
#             height=self.height,
#             xlabel=self.xlabel,
#             ylabel=self.ylabel,
#             limits=self.limits,
#             xscale=self.xscale,
#             yscale=self.yscale,
#             ticks=self.ticks,
#             axis_equal=self.axis_equal,
#             scalebar=self.scalebar,
#             colorbar=self.colorbar,
#             colormap=self.colormap,
#             external_filename=self.external_filename,
#             font_options=self.font_options,
#             axis_options=self.axis_options,
#             has_graph_paper=self.has_graph_paper,
#             plot=self,
#             plot_template=self.template)
#         return response


# Path: demo/demo_fourier_with_legend.py
import math
import numpy as np
from artist import Plot



def add_custom_pin(plot, x, y, label, distance='3ex'):
    """Add a pin at the maximum value of a data series (sort of)"""

    # only use the second quarter, for cosmetic reasons
    q = int(0.25 * len(y))
    y = y[q:2*q]
    max_index = q + np.argmax(y)
    max_x = x[max_index]
    plot.add_pin(label, x=max_x, location='above', use_arrow=True,
                 style='pin distance=%s' % distance)


X = np.linspace(0., 2 * L, num=1000)
Y_sqr = [square(x) for x in X]
Y = lambda n: [fourier(x, n) for x in X]

graph = Plot()
graph.set_title("Fourier approximation")

graph.plot(x=X, y=Y(3), linestyle='red', mark=None, legend='n=3')
graph.plot(x=X, y=Y(5), linestyle='yellow', mark=None, legend='n=5')
graph.plot(x=X, y=Y(8), linestyle='green', mark=None, legend='n=8')
graph.plot(x=X, y=Y(13), linestyle='blue', mark=None, legend='n=13')
graph.plot(x=X, y=Y(55), linestyle='cyan', mark=None, legend='n=55')
graph.plot(x=X, y=Y_sqr, linestyle='black', mark=None, legend='square')

graph.save('fourier_with_legend')


graph = Plot()