Skip to content

3. Plot Builder example

Lets explore the PlotBuilder capabilities in search of an awful plot. We will use the same function from last example to create some data:

import numpy as np
import matplotlib.pyplot as plt
import itfit

def gauss(x, A, x0, sigma):
    return A * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))

def dataFunction(x, m, n, A, x0, sigma):
    return m*x + n + gauss(x, A ,x0, sigma)

noise = np.random.normal(size=200)

xdata = np.arange(200)
ydata = dataFunction(xdata, -0.04, 5, 20, 105, 15) + noise
sigma = np.random.normal(size=200)*1.5
Now we call the Fitter and fit the data to a gaussian predefined function.
fitter_app = itfit.Fitter(xdata, ydata, yerr=sigma)

fitter_app()
plt.show()
Once we fit the data we call the PlotBuilder. This class is very usefull if you are using a jupyther notebook as changes are made instantly over the same figure. The main think to keep in mind when using the PlotBuilder is that all methods return the PlotBuilder instance, therefore you can chain changes until you get the desired result.

PlotBuilder is more limited than using Matplotlib's figure and axes, but it is easier to plot the fit with the corresponding data. Although figure and axe are accesible via .fig and .ax attributes.

fit = fitter_app.get_plot_builder()\
    .style("science")\
    .plot_fit(':', 'red', 'test', only_selected=True)\
    .with_data('.', 'black', 'data')\
    .xlabel("time [s]")\
    .ylabel("current [mA]")\
    .title("Experiment")\
    .grid()\
    .legend()\
    .set_xlim(-10, 220)\
    .set_ylim(-10, 30)\
    .spines().start_top_spine().invisible().end_top_spine()\
             .start_right_spine().alpha(0.33).color("white").end_right_spine()\
             .start_bottom_spine().linestyle('--').color('purple').linewidth(3).end_bottom_spine()\
    .end_spines()\
    .set_size((12,9))\
    .tight_layout()\
    .save_fig("this_is_not_ok.svg")

image