Beispiel #1
0
    def test_axes_imshow_warnings(self):
        from pgmpl.axes import AxesImage
        a = self.rgb2d
        ax = Axes()

        warnings_expected = {'0.10': 8, '0.11': 8, '0.12': 10}[pgvmm]
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger warnings.
            img = ax.imshow(data={
                'x': a,
                'unrecognized': 'thingy'
            },
                            shape=np.shape(a),
                            imlim=55,
                            interpolation='nearest',
                            filternorm=2,
                            filterrad=5.0,
                            resample=True,
                            url='google.com',
                            blah=True)  # 8 warnings
            # Verify that warnings were made.
            warnlist = '\n'.join([
                f"{i+1}: {ww.message} in {ww.filename}:{ww.lineno}"
                for i, ww in enumerate(w)
            ])
            if len(w) != warnings_expected:
                print(
                    f'\nExpected {warnings_expected} warnings and detected {len(w)} warnings:\n{warnlist}\n'
                )
            self.assertEqual(len(w), warnings_expected,
                             'Number of warnings does not match expectation')
        assert isinstance(
            img, AxesImage
        )  # It should still return the instance using the implemented keywords.
        self.printv(
            '      test_axes_imshow_warnings: tried to call Axes.imshow instance using unimplemented '
            'keywords and got {}/{} warnings. img = {}'.format(
                len(w), warnings_expected, img))
Beispiel #2
0
 def test_axes_plot(self):
     ax = Axes()
     ax.plot(self.x, self.y, color='r')
     for i in range(
             len(ax.prop_cycle) + 1
     ):  # Use up all the colors in the prop cycle so it has to loop
         ax.plot(self.x, self.y + i)
Beispiel #3
0
    def test_axes_aspect(self):
        ax = Axes()
        ax.plot([0, 10, 0, 1])
        ax.set_aspect('equal')
        ax.set_aspect(16.0 / 9.0)

        # Test warnings
        warnings_expected = 3
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger a warning.
            ax.set_aspect('equal',
                          adjustable='datalim',
                          anchor='C',
                          share=True)
            # Verify that warnings were made.
            assert len(w) == warnings_expected
        self.printv(
            '      test_axes_aspect: tried to call Axes set_aspect() using unimplemented keywords '
            'and got {}/{} warnings. ax = {}'.format(len(w), warnings_expected,
                                                     ax))
Beispiel #4
0
    def test_axes_legend_warnings(self):
        ax = Axes()
        ax.plot(self.x, self.y, label='y(x) plot')
        leg = ax.legend()

        # Test warnings
        warnings_expected = 5
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger warnings.
            leg.draggable(False)  # 1 warning
            # Trigger more warnings:
            ax.legend(blah='unrecognized keyword should make warning',
                      borderaxespad=5)  # 2 warnings
            ax.legend(loc=0)  # 1 warning
            ax.legend(loc=4)  # 1 warning
            # Verify that warnings were made.
        self.printv(
            '      test_axes_Legend: triggered a warning from Legend and got {}/{} warnings. leg = {}'
            .format(len(w), warnings_expected, leg))
        assert len(w) == warnings_expected
Beispiel #5
0
 def add_subplot(self, nrows, ncols, index, **kwargs):
     """Imitation of matplotlib.figure.Figure.add_subplot"""
     check_unimplemented_keywords(['projection', 'polar'],
                                  method='add_subplot',
                                  **kwargs)
     row = int(np.floor((index - 1) / ncols))
     if row > (nrows - 1):
         raise ValueError(
             'index {} would be on row {}, but the last row is {}!'.format(
                 index, row, nrows - 1))
     col = (index - 1) % ncols
     ax = Axes(nrows=nrows, ncols=ncols, index=index, **kwargs)
     self.layout.addItem(ax, row + 1, col)
     self._deleted_axes_protection('add_subplot')
     self.axes = ax if self.axes is None else tolist(self.axes) + [ax]
     self.fig_colspan = max([ncols, self.fig_colspan])
     self.refresh_suptitle()
     return ax
Beispiel #6
0
    def colorbar(self, mappable, cax=None, ax=None, **kwargs):
        ax = ax or self.gca()
        if cax is None:
            orientation = kwargs.get('orientation', 'vertical')
            row = int(np.floor((ax.index - 1) / ax.ncols))
            col = (ax.index - 1) % ax.ncols

            cax = Axes(nrows=ax.nrows, ncols=ax.ncols, index=ax.index)
            sub_layout = pg.GraphicsLayout()
            sub_layout.addItem(ax, row=0, col=0)
            sub_layout.addItem(cax,
                               row=int(orientation == 'horizontal'),
                               col=int(orientation != 'horizontal'))
            if orientation == 'horizontal':
                sub_layout.layout.setRowFixedHeight(
                    1, 50)  # https://stackoverflow.com/a/36897295/6605826
            else:
                sub_layout.layout.setColumnFixedWidth(
                    1, 50)  # https://stackoverflow.com/a/36897295/6605826

            self._try_remove_from_layout(ax)
            self.layout.addItem(sub_layout, row + 1, col)
        return Colorbar(cax, mappable, **kwargs)
Beispiel #7
0
 def test_axes_scatter(self):
     ax = Axes()
     ax.scatter(self.x, -self.y)
     ax.scatter(self.x, self.y, c=self.z)
     ax.scatter(self.x, self.y, c='b', aspect='equal')
     ax.scatter(self.x,
                self.y,
                c=self.z,
                cmap='plasma',
                marker='s',
                linewidths=1,
                edgecolors='r')
     # noinspection PyTypeChecker
     ax.scatter(self.x,
                self.x * 0,
                c=self.x,
                cmap='jet',
                marker=None,
                verts=[(0, 0), (0.5, 0.5), (0, 0.5), (-0.5, 0), (0, -0.5),
                       (0.5, -0.5)])
     ax.scatter(data={'x': self.x, 'y': self.y, 'c': self.z, 's': 10})
Beispiel #8
0
 def test_axes_clear(self):
     ax = Axes()
     ax.plot(self.y, self.x)  # Switch them so the test doesn't get bored.
     ax.clear()
Beispiel #9
0
 def test_axes_xyaxes_warnings(self):
     ax = Axes()
     ax.set_xlim(1, 10)
     ax.set_ylim(1, 10)
     warnings_expected = 8
     with warnings.catch_warnings(record=True) as w:
         # Cause all warnings to always be triggered.
         warnings.simplefilter("always")
         # Trigger warnings.
         ax.set_xscale('symlog', keyword='unrecognized')  # 2 warnings
         ax.set_xscale('logit')  # 1 warning
         ax.set_xscale('unrecognized')  # 1 warning
         ax.set_yscale('symlog', keyword='unrecognized')  # 2 warnings
         ax.set_yscale('logit')  # 1 warning
         ax.set_yscale('unrecognized')  # 1 warning
         # Verify that warnings were made.
         assert len(w) == warnings_expected
     self.printv(
         '      test_axes_xyaxes_warnings: tried to make probelmatic calls to Axes lim & scale methods '
         'and got {}/{} warnings. ax = {}'.format(len(w), warnings_expected,
                                                  ax))
Beispiel #10
0
 def test_axes_xyaxes(self):
     ax = Axes()
     ax.plot([1, 5], [1, 2])
     ax.set_ylabel('ylabel')
     ax.set_xlabel('xlabel')
     ax.set_title('title title title')
     ax.set_xlim()
     ax.set_ylim()
     ax.set_xlim([-1, 2])
     ax.set_ylim([-2, 4])
     ax.set_xlim(1, 2)
     ax.set_ylim(1, 4)
     ax.set_xscale('linear')
     ax.set_yscale('linear')
     ax.set_xscale('log')
     ax.set_yscale('log')
Beispiel #11
0
 def test_axes_err(self):
     ax = Axes()
     yerr = self.y * 0.1
     ax.errorbar(-self.x, self.y, yerr, color='r')
     ax.errorbar(self.x,
                 self.y,
                 yerr,
                 color='r',
                 capsize=6,
                 capthick=1.25,
                 marker='s',
                 ecolor='m')
     ax.errorbar(data={'x': -self.x, 'y': -self.y, 'yerr': yerr})
     ax.errorbar(self.x,
                 self.y,
                 yerr,
                 elinewidth=2.5,
                 markeredgewidth=1.2,
                 barsabove=True)
     ax.errorbar(self.x, self.y - 20, yerr, capsize=0, capthick=0)
     ax.fill_between(self.x, -self.y - yerr - 1, -self.y + yerr - 1)
     ax.fill_between(data={
         'x': -self.x,
         'y1': 10 - self.y - yerr - 1,
         'y2': -self.y + yerr - 1
     })
     ax.fill_between(self.x,
                     -self.y - yerr - 1,
                     -self.y + yerr - 1,
                     where=(self.x >= np.mean(self.x)))
     ax.fill_between(self.x, self.y + 20, 0)
Beispiel #12
0
 def test_axes_warnings(self):
     ax = Axes()
     warnings_expected = 9
     with warnings.catch_warnings(record=True) as w:
         # Cause all warnings to always be triggered.
         warnings.simplefilter("always")
         # Trigger warnings.
         ax.text(0.5, 0.91, 'text1', withdash=True)  # 1 warning
         ax.fill_between(self.x, self.y * 0.1, self.y * 1.1,
                         step='pre')  # 1 warning
         ax.fill_between(self.x,
                         -self.y - 10,
                         self.y - 10,
                         where=(self.x >= np.mean(self.x)),
                         interpolate=True)  # 1 warning
         ax.set_xlim([-1, 2], emit=False, auto=True,
                     blah=True)  # 3 warnings
         ax.set_ylim([-1, 2], emit=False, auto=True,
                     blah=True)  # 3 warnings
         # Verify that warnings were made.
         assert len(w) == warnings_expected
     self.printv(
         '      test_axes_warnings: tried to make probelmatic calls to Axes methods '
         'and got {}/{} warnings. ax = {}'.format(len(w), warnings_expected,
                                                  ax))
Beispiel #13
0
 def test_manual_legend(self):
     ax = Axes()
     line = ax.plot(self.x, self.y)
     line2 = ax.plot(self.x, self.y * 2, color='r')
     ax.legend([line, line2], ['line 1', 'line 2'])
Beispiel #14
0
    def test_axes_legend(self):
        """Test Legend class through axes method legend"""
        ax = Axes()
        line = ax.plot(self.x, self.y, label='y(x) plot')
        leg = ax.legend()
        leg.addItem(line, name='yx plot')
        leg.draggable()
        leg.clear()
        ax2 = Axes()
        ax2.plot(self.x, self.y, color='r', label='y(x) plot red')
        ax2.plot(self.x, -self.y, color='b', label='y(x) plot blue')
        ax2.legend(labels='blah')
        leg.addItem(line, name='yx plot again')
        leg.removeItem(line)

        self.printv('test_axes_Legend: ax = {}, leg = {}'.format(ax, leg))
Beispiel #15
0
 def test_contour_errors(self):
     ax = Axes()
     self.assertRaises(TypeError, ax.contour, self.x, self.y, self.z, self.levels, self.nlvl)
Beispiel #16
0
 def test_axes_imshow(self):
     from pgmpl.axes import AxesImage
     a = self.rgb2d
     ax = Axes()
     img = ax.imshow(a)
     ax1 = Axes()
     img1 = ax1.imshow(data={'x': a[:, :, 0:2]}, aspect='equal')
     ax2 = Axes()
     img2 = ax2.imshow(self.two_d_data)
     assert isinstance(img, AxesImage)
     assert isinstance(img1, AxesImage)
     assert isinstance(img2, AxesImage)
     ax.imshow(data={'x': a})
     self.printv(
         '      test_axes_imshow: ax = {}, ax1 = {}, ax2 = {}, img = {}, img1 = {}, img2 = {}'
         .format(ax, ax1, ax2, img, img1, img2))
Beispiel #17
0
 def test_axes_contour(self):
     a = sum(self.rgb2d, 2) * 10
     levels = [0, 0.5, 1.2, 5, 9, 10, 20, 30]
     ax = Axes()
     ax.contour(a)
     ax2 = Axes()
     ax2.contour(a, levels)
     ax3 = Axes()
     ax3.contour(a, 3)
Beispiel #18
0
 def test_axes_lines(self):
     ax = Axes()
     ax.axhline(0.5, linestyle='--', color='k')
     ax.axvline(0.5)
     ax.axvline(0.75, linestyle='-', color='b')