Exemple #1
0
    def test_styling_hierarchy(self, fig_test, fig_ref):
        # dev-test
        # Test styling hierarchy: Block/Flow > Tags > SubDiagram > Alluvial
        # set styling
        style = dict(ec='green', lw=2)
        alluv_c, subd_c, tag_c, block_c = 'blue', 'green', 'red', 'yellow'
        # create the two figures
        tesax = fig_test.subplots()
        refax = fig_ref.subplots()
        # draw a simple Recangle on refax
        yoff = 4
        # ###
        # refax
        pc = []
        # Alluvial style
        pc.append(
            Rectangle((-.5, yoff), width=1, height=1, fc=alluv_c, **style))
        # SubD style
        pc.append(Rectangle((-0.5, 0), width=1, height=3, fc=subd_c, **style))
        # Tag style
        pc.append(Rectangle((1.5, 0), width=1, height=3, fc=tag_c, **style))
        # Block style
        pc.append(
            Rectangle((1.5, yoff), width=1, height=1, fc=block_c, **style))
        refax.add_collection(PatchCollection(pc, match_original=True))
        # ###
        # texax
        # set fc to blue for the entire alluvial plot
        style['width'] = 1
        alluvial = Alluvial(x=[0, 2], ax=tesax, fc=alluv_c, blockprops=style)
        # Test defaults form Alluvial:
        alluvial.add(flows=None, ext=[1], yoff=4, layout='bottom', **style)
        # Test SubD > Alluvial:
        diagram1 = alluvial.add(flows=None,
                                ext=[[3], [3, 1]],
                                layout='bottom',
                                fc=subd_c,
                                **style)
        # Tag > SubD:
        tag = alluvial.register_tag('tag0', fc=tag_c)
        alluvial.tag_blocks(tag, 1, 1, None)
        # Block  > Tag:
        # set the styling of a single block in an already styled subdiagram
        block = diagram1.get_block((1, 1))  # column 1, block 0
        block.set_facecolor(block_c)
        # block.set_property('facecolor', block_c)
        alluvial.finish()
        # ###

        # set common limits and axis styling
        tesax.set_xlim(-1, 4)
        tesax.set_ylim(-1, 6)
        refax.set_xlim(*tesax.get_xlim())
        refax.set_ylim(*tesax.get_ylim())
        refax.xaxis.set_major_locator(tesax.xaxis.get_major_locator())
        refax.set(frame_on=False)
        refax.set(yticks=[])
        plt.close('all')
Exemple #2
0
 def test_alluvial_creation(self, ext, flows, fractionflow, layout):
     # test creation of alluvial diagram with add and finish
     alluvial = Alluvial()
     alluvial.add(flows=flows,
                  ext=ext,
                  fractionflow=fractionflow,
                  layout=layout,
                  blockprops=dict(width=1))
     alluvial.finish()
Exemple #3
0
 def test_multiple_subdiagrams(self, ext, flows, fractionflow, layout):
     # dev-test
     # several sub diagrams with add and finish
     alluvial = Alluvial()
     alluvial.add(flows=flows,
                  ext=ext,
                  fractionflow=fractionflow,
                  layout=layout,
                  blockprops=dict(width=1))
     alluvial.add(flows=flows,
                  ext=ext,
                  fractionflow=fractionflow,
                  yoff=2,
                  layout=layout,
                  blockprops=dict(width=1))
     alluvial.finish()
Exemple #4
0
    def test_Block_styling(self, fig_test, fig_ref):
        # Check individual styling of Rectangles.
        style = dict(ec='green', lw=2, clip_on=True)
        # create the two figures
        tesax = fig_test.subplots()
        refax = fig_ref.subplots()
        # ###
        # refax
        # draw a simple Recangle on refax
        pc = []
        pc.append(Rectangle((-0.5, 0), width=1, height=3, **style))
        pc.append(Rectangle((-0.5, 4), width=1, height=1, **style))
        # custom styling for a single rectangle
        style['ec'] = 'black'
        pc.append(Rectangle((1.5, 0), width=1, height=2, fc='red', **style))
        style['ec'] = 'green'
        refax.add_collection(PatchCollection(pc, match_original=True,
                                             zorder=4))
        # ###
        # tesax
        # draw an alluvial with 1 diagram 2 cols, 3 blocks and no flows
        alluvial = Alluvial(x=[0, 2],
                            ax=tesax,
                            blockprops=dict(width=1, ha='center'))
        diagram0 = alluvial.add(flows=None,
                                ext=[[3, 1], [2]],
                                layout='bottom',
                                blockprops=style)
        # set the styling of a single block
        block = diagram0.get_block((1, 0))  # column 1, block 0
        block.set_facecolor('red')
        block.set_ec('black')
        # block.set_property('facecolor', 'red')
        alluvial.finish()
        # ###

        # set common limits and axis styling
        refax.set_xlim(*tesax.get_xlim())
        refax.set_ylim(*tesax.get_ylim())
        refax.xaxis.set_major_locator(tesax.xaxis.get_major_locator())
        refax.set(frame_on=False)
        refax.set(yticks=[])
        plt.close('all')
Exemple #5
0
memberships = [
    [0, 1, 1, 2],  # t1
    [3, 0, 1, 2],  # t2
    [1, 0, 1, 1]
]  # t3

layouts = ['centered', 'bottom', 'top']
y_offsets = [0, 3, -3]
alluv = Alluvial(x=['t1', 't2', 't3'], width=0.4)
for layout, yoff in zip(layouts, y_offsets):
    # Create an alluvial diagram based on the memberships using a centered layout
    alluv.add_from_memberships(memberships, layout=layout, yoff=yoff)
# TODO: Adding labels for sub-diagrams
# Tell alluvial to determine the layout and draw the diagrams
alluv.finish()

plt.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions, methods, classes and modules is shown
# in this example:

import matplotlib
matplotlib.alluvial
Exemple #6
0
    def test_cmap_usage(self, fig_test, fig_ref):
        # dev-test
        # Tests:
        # - usage of colormaps for subdiagrams and tags
        # - using datetime on x axis
        from datetime import datetime, timedelta
        from matplotlib import cm
        single_c = 'yellow'
        reds = cm.get_cmap("Reds")
        blues = cm.get_cmap("Blues")
        # first convert list of colors to get colors for ref_figure
        nbr_blocks = 3  # will use 3 blocks
        reds_l = reds(np.linspace(0, 1, nbr_blocks))
        blues_l = blues(np.linspace(0, 1, nbr_blocks))
        print('reds', reds_l)
        print('blues', blues_l)
        style = dict(ec='black', lw=2, width=timedelta(days=1))
        yoff = 4
        # ###
        # refax
        refax = fig_ref.subplots()
        # draw 6 Recangles 3 top ones with 'Blues', 3 bottom ones with 'Reds'
        # x = [0, 2, 4]
        x = [datetime(2020, 1, 1), datetime(2020, 1, 3), datetime(2020, 1, 5)]
        _x = 3 * x
        heights = [1, 2, 1, 3, 3, 2, 1, 1, 1]
        yoff = [4, 4, 3, 0, 0, 0, 7, 7, 7]
        c_l = list(blues_l) + 3 * [single_c] + list(reds_l)
        for i in range(9):
            refax.add_patch(
                Rectangle((_x[i], yoff[i]),
                          height=heights[i],
                          fc=c_l[i],
                          **style))
            # refax.add_collection(PatchCollection(pc, match_original=True))

        # TODO: separate test for cmap on sub-diagram and cmap on tag
        # ###
        # texax
        tesax = fig_test.subplots()
        style['ha'] = 'left'
        style['va'] = 'bottom'
        alluv = Alluvial(x=x, ax=tesax, blockprops=style, layout='bottom')
        alluv.add(flows=None,
                  ext=[*zip(heights[:3], heights[3:6])],
                  fc=single_c,
                  hspace_combine='add')
        # create a tag for the reds
        alluv.register_tag('tag0', cmap=blues, mappable='x')
        # alluv.register_tag('tag0')
        alluv.tag_blocks('tag0', 0, None, -1)  # get top block in all cols
        alluv.add(flows=None,
                  ext=[*zip(heights[6:])],
                  blockprops=dict(cmap=reds, mappable='x'),
                  yoff=7)
        alluv.finish()
        # ###
        # set common limits and axis styling
        refax.set_xlim(*tesax.get_xlim())
        refax.set_ylim(*tesax.get_ylim())
        refax.xaxis.set_major_locator(tesax.xaxis.get_major_locator())
        refax.xaxis.set_major_formatter(tesax.xaxis.get_major_formatter())
        refax.set(frame_on=False)
        refax.set(yticks=[])
        plt.close('all')