Ejemplo n.º 1
0
    def show_ratio_box(self, ratio, area, start, end, copy_to_clipboard=True):
        """Displays a text box that shows the ratio and of areas between two
        spectra within start and end.
        """
        if self.anchored_box:
            self.anchored_box.set_visible(False)
            self.anchored_box = None

        child_boxes = []

        if ratio is None:
            text = "Invalid selection, \nno ratio could \nbe calculated"
            child_boxes.append(
                offsetbox.TextArea(text, textprops=dict(color="k", size=12)))

        else:
            ratio_round = 9  # Round decimal number

            text = f"Difference: {round(area, 2)}\n" \
                   f"Ratio: {round(ratio, ratio_round)}\n" \
                   f"Interval: [{round(start, 2)}, {round(end, 2)}]"
            child_boxes.append(
                offsetbox.TextArea(text, textprops=dict(color="k", size=12)))

            if copy_to_clipboard:
                self.clipboard.setText(str(round(ratio, ratio_round)))
                text_2 = "\nRatio copied to clipboard."
                child_boxes.append(
                    offsetbox.TextArea(text_2,
                                       textprops=dict(color="k", size=10)))

        box = offsetbox.VPacker(children=child_boxes,
                                align="center",
                                pad=0,
                                sep=0)

        self.anchored_box = offsetbox.AnchoredOffsetbox(
            loc=2,
            child=box,
            pad=0.5,
            frameon=False,
            bbox_to_anchor=(1.0, 1.0),
            bbox_transform=self.axes.transAxes,
            borderpad=0.,
        )
        self.axes.add_artist(self.anchored_box)
        self.axes.add_artist(self.leg)
        self.canvas.draw_idle()
Ejemplo n.º 2
0
 def __init__(self, length=1, extent = 0.03, label="", loc=2, ax=None,
              pad=0.4, borderpad=0.5, ppad = 0, sep=2, prop=None, 
              frameon=True, linekw={}, **kwargs):
     if not ax:
         ax = plt.gca()
     trans = ax.get_xaxis_transform()
     size_bar = offbox.AuxTransformBox(trans)
     line = Line2D([0,length],[0,0], **linekw)
     size_bar.add_artist(line)
     txt = offbox.TextArea(label, minimumdescent=False, 
                           textprops=dict(color="black",size=14, fontweight='bold'))
     self.vpac = offbox.VPacker(children=[size_bar,txt],  
                              align="center", pad=ppad, sep=sep) 
     offbox.AnchoredOffsetbox.__init__(self, loc, pad=pad, 
              borderpad=borderpad, child=self.vpac, prop=prop, frameon=frameon,
              **kwargs)
Ejemplo n.º 3
0
def plot_lines(data,
               labels,
               xlabel,
               ylabel,
               title,
               suptitle,
               conf=None,
               additionals=None,
               path=None,
               ls='-',
               begin=1):
    fig, ax = plt.subplots()
    fig.set_size_inches(15, 10)
    for i, d in enumerate(data):
        ax.plot(range(begin,
                      len(d) + begin),
                d,
                color=colors[i],
                ls=ls,
                label=labels[i])
    legend = ax.legend(framealpha=.3)

    if conf is not None:
        import matplotlib.offsetbox as offsetbox
        keys = []
        vals = []
        for k, v in conf.items():
            if k != 'features':
                if v is None:
                    v = "none"
                elif type(v) == bool:
                    if v == True:
                        v = "true"
                    else:
                        v = "false"
                if k == 'n_input':
                    keys.append(offsetbox.TextArea(r"$features$"))
                    vals.append(
                        offsetbox.TextArea(r"${}$".format(v),
                                           textprops={'size': 10}))
                elif k == 'unit_range':
                    if v is not None:
                        keys.append(offsetbox.TextArea(r"$unit range$"))
                        vals.append(
                            offsetbox.TextArea(r"${}-{}$".format(v[0], v[1]),
                                               textprops={'size': 10}))
                elif k == 'units':
                    keys.append(offsetbox.TextArea(r"$hidden\_layers$"))
                    vals.append(
                        offsetbox.TextArea(r"${}$".format(len(v)),
                                           textprops={'size': 10}))
                    for l, layer in enumerate(v):
                        keys.append(
                            offsetbox.TextArea(r"$units_{HL{%d}}$" % (l)))
                        vals.append(
                            offsetbox.TextArea(r"${}$".format(layer),
                                               textprops={'size': 10}))
                elif k == 'learning_rate':
                    keys.append(offsetbox.TextArea(r"$\eta$"))
                    vals.append(
                        offsetbox.TextArea(r"${}$".format(v),
                                           textprops={'size': 10}))
                else:
                    keys.append(
                        offsetbox.TextArea(r"${}$".format(k.replace("_",
                                                                    "\_"))))
                    vals.append(
                        offsetbox.TextArea(r"${}$".format(v),
                                           textprops={'size': 10}))

        if additionals is not None:
            keys.append(
                offsetbox.TextArea(r"$E_{min, train}$",
                                   textprops={'color': colors[0]}))
            keys.append(
                offsetbox.TextArea(r"$E_{min, valid}$",
                                   textprops={'color': colors[1]}))
            vals.append(
                offsetbox.TextArea(r"${:.2e} {} (epoch {})$".format(
                    additionals[0][1], " ", additionals[0][0]),
                                   textprops={
                                       'color': colors[0],
                                       'size': 10,
                                       'weight': 'bold'
                                   }))
            vals.append(
                offsetbox.TextArea(r"${:.2e} {} (epoch {})$".format(
                    additionals[1][1], " ", additionals[1][0]),
                                   textprops={
                                       'color': colors[1],
                                       'size': 10,
                                       'weight': 'bold'
                                   }))

        vp1 = offsetbox.VPacker(children=keys, align="left", pad=0, sep=3)
        vp2 = offsetbox.VPacker(children=vals, align="right", pad=0, sep=5)
        hp = offsetbox.HPacker(children=(vp1, vp2),
                               align="right",
                               pad=5.76,
                               sep=28.8)
        box = legend._legend_box
        box.get_children()[1].get_children()[0].get_children().append(hp)
        box.set_figure(box.figure)

    if additionals is not None:
        add = np.array(additionals).T
        plt.vlines(x=add[0],
                   ymin=0,
                   ymax=add[1],
                   colors=colors,
                   linestyles='dotted')

    plt.xlabel(xlabel)
    plt.ylabel(ylabel if ylabel not in ylabels else ylabels[ylabel])
    plt.xlim(xmax=len(data[0]))
    plt.title(title)
    if path is not None:
        plt.savefig(path, dpi=300)
    plt.show()