Examples

hello world

../examples/example_hello_world_output.svg
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt

plt.ion()
from figurefirst import FigureLayout

layout = FigureLayout("example_hello_world_layout.svg")
layout.make_mplfigures()

d = np.array(
    [
        [144, 57],
        [138, 57],
        [138, 59],
        [141, 61],
        [141, 82],
        [138, 84],
        [138, 85],
        [142, 85],
        [147, 85],
        [147, 84],
        [144, 82],
        [144, 57],
        [144, 57],
        [155, 57],
        [149, 57],
        [149, 59],
        [152, 61],
        [152, 82],
        [149, 84],
        [149, 85],
        [153, 85],
        [158, 85],
        [158, 84],
        [155, 82],
        [155, 57],
        [155, 57],
        [273, 57],
        [267, 57],
        [267, 59],
        [270, 61],
        [270, 82],
        [267, 84],
        [267, 85],
        [271, 85],
        [276, 85],
        [276, 84],
        [273, 82],
        [273, 57],
        [273, 57],
        [295, 57],
        [289, 57],
        [289, 59],
        [292, 61],
        [292, 70],
        [287, 67],
        [278, 76],
        [287, 85],
        [292, 83],
        [292, 85],
        [298, 85],
        [298, 84],
        [295, 81],
        [295, 57],
        [295, 57],
        [90, 57],
        [90, 59],
        [91, 59],
        [94, 61],
        [94, 82],
        [91, 84],
        [90, 84],
        [90, 85],
        [96, 85],
        [102, 85],
        [102, 84],
        [101, 84],
        [98, 82],
        [98, 71],
        [110, 71],
        [110, 82],
        [107, 84],
        [106, 84],
        [106, 85],
        [112, 85],
        [118, 85],
        [118, 84],
        [117, 84],
        [113, 82],
        [113, 61],
        [117, 59],
        [118, 59],
        [118, 57],
        [112, 58],
        [106, 57],
        [106, 59],
        [107, 59],
        [110, 61],
        [110, 70],
        [98, 70],
        [98, 61],
        [101, 59],
        [102, 59],
        [102, 57],
        [96, 58],
        [90, 57],
        [90, 57],
        [193, 57],
        [193, 59],
        [197, 60],
        [205, 85],
        [205, 86],
        [206, 85],
        [213, 65],
        [219, 85],
        [220, 86],
        [221, 85],
        [229, 61],
        [233, 59],
        [233, 57],
        [229, 58],
        [224, 57],
        [224, 59],
        [228, 61],
        [227, 62],
        [221, 80],
        [215, 60],
        [215, 60],
        [218, 59],
        [218, 57],
        [213, 58],
        [208, 57],
        [208, 59],
        [211, 60],
        [212, 63],
        [207, 80],
        [200, 60],
        [200, 60],
        [203, 59],
        [203, 57],
        [198, 58],
        [193, 57],
        [193, 57],
        [128, 67],
        [120, 76],
        [129, 85],
        [135, 80],
        [135, 80],
        [134, 80],
        [129, 84],
        [125, 82],
        [123, 76],
        [134, 76],
        [135, 75],
        [128, 67],
        [128, 67],
        [169, 67],
        [160, 76],
        [169, 85],
        [178, 76],
        [169, 67],
        [169, 67],
        [240, 67],
        [231, 76],
        [240, 85],
        [249, 76],
        [240, 67],
        [240, 67],
        [257, 67],
        [251, 68],
        [251, 69],
        [254, 71],
        [254, 82],
        [251, 84],
        [251, 85],
        [256, 85],
        [261, 85],
        [261, 84],
        [260, 84],
        [257, 82],
        [257, 75],
        [262, 68],
        [262, 68],
        [261, 70],
        [263, 71],
        [265, 70],
        [262, 67],
        [257, 71],
        [257, 67],
        [257, 67],
        [128, 68],
        [133, 75],
        [123, 75],
        [128, 68],
        [128, 68],
        [169, 68],
        [173, 70],
        [174, 76],
        [173, 81],
        [169, 84],
        [164, 82],
        [163, 76],
        [164, 70],
        [169, 68],
        [169, 68],
        [240, 68],
        [244, 70],
        [246, 76],
        [245, 81],
        [240, 84],
        [235, 82],
        [234, 76],
        [235, 70],
        [240, 68],
        [240, 68],
        [287, 68],
        [292, 70],
        [292, 72],
        [292, 80],
        [292, 82],
        [287, 84],
        [283, 82],
        [281, 76],
        [283, 71],
        [287, 68],
        [287, 68],
    ]
)

ax = layout.axes["ax_name"]["axis"]

ax.plot(d[:, 0], -d[:, 1], lw=4)

layout.insert_figures("target_layer_name")

layout.write_svg("example_hello_world_output.svg")

grouped axes

../examples/example_group_axes_output.svg
#!/usr/bin/env python
from pylab import *
from figurefirst import FigureLayout, mpl_functions

# Group axes example
layout = FigureLayout("example_group_axes_layout.svg")
layout.make_mplfigures()
layout.insert_figures()
layout.write_svg("example_group_axes_output.svg")
close("all")

nested groups

../examples/example_nested_groups_output.svg
#!/usr/bin/env python
import figurefirst as fifi
import matplotlib.pyplot as plt
import matplotlib

layout = fifi.FigureLayout("example_nested_groups_layout.svg")
layout.make_mplfigures()
# print layout.axes
# layout.axes_groups['fig2']['group3']['ax2'].plot([2,3,4])
for key, ax in layout.axes.items():
    # print key
    # print type(ax)
    ax.plot([1, 3, 2, 4])

cdict = {"r1": 0.3, "r2": 0.1, "r3": 0.9}
for key, value in cdict.items():
    hexi = matplotlib.colors.rgb2hex(plt.cm.viridis(value))
    layout.svgitems["svggroup"][key].style["fill"] = str(hexi)

layout.apply_svg_attrs()
layout.insert_figures()
layout.write_svg("example_nested_groups_output.svg")

axis methods

../examples/example_axis_methods_output.svg
#!/usr/bin/env python
from pylab import *
from figurefirst import FigureLayout, mpl_functions

# You can decorate the svg <figurefirsrt:axis> tag with mpl.axis methods. For example to call ax.axhspan(100,200,zorder=10,color ='r',alpha = 0.3) on the axis named frequency.22H05.start use the following tag:
#
# ```
# <figurefirst:axis
#      figurefirst:name="frequency.22H05.start"
#      figurefirst:axhspan="100,200,zorder=10,color='r',alpha=.3"/> ```
#
# The layout.apply_mpl_methods function will then apply the methods passing the value of the svg atribute as arguments to the mpl.axis method.

# Passing axis methods
import numpy as np

layout = FigureLayout("example_axis_methods_layout.svg")
layout.make_mplfigures()
layout.fig.set_facecolor("None")
for mplax in layout.axes.values():
    ax = mplax["axis"]
    ax.plot(np.arange(30), np.random.rand(30), color="k")
    mpl_functions.adjust_spines(
        ax,
        "none",
        spine_locations={},
        smart_bounds=True,
        xticks=None,
        yticks=None,
        linewidth=1,
    )
    ax.patch.set_facecolor("None")
layout.apply_mpl_methods()
layout.insert_figures("mpl_panel_a")
layout.write_svg("example_axis_methods_output.svg")
close("all")

figure templating

../examples/example_figure_templating_output.svg
#!/usr/bin/env python
import figurefirst

"""
To create a templated figure, draw a rectangle, and add a figurefirst:figure tag as if you were creating another figure. 
Then in addition to the figurefirst:name attribute, add a figurefirst:template attribute, 
whose value should correspond to the name of the figurefirst figure you want to use as the template. 
The template figure will be scaled to fill the box. 
"""

layout = figurefirst.FigureLayout("example_figure_templating_layout.svg")
layout.make_mplfigures()
layout.append_figure_to_layer(layout.figures["group2"], "mpl_layer_2")
layout.append_figure_to_layer(layout.figures["group3"], "mpl_layer_3")
layout.write_svg("example_figure_templating_output.svg")