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')
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()
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()
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')
from matplotlib import pyplot as plt # from matplotlib.alluvial import Alluvial from pyalluv import Alluvial import numpy as np ext = np.array([1, 2, 1]) flows = np.array([[[0, 1, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0, 0], [1, 0, 1, 1]]]) alluv = Alluvial(x=['t1', 't2', 't3']) alluv.add(flows=flows, ext=ext, yoff=0, layout='top', blockprops=dict(width=0.2, show_label=True), flowprops=dict(show_label=True, labelprops=dict(loc='left')) ) ax = alluv.ax for i, block in enumerate(alluv.select_blocks(0, None, slice(0, 2))): block.set_facecolor('green') # set the label block.set_label(f'block {i}') if block.outflows: f = block.outflows[0] f.set_label('first out') elif block.inflows: f = block.inflows[-1] f.set_facecolor('red') f.set_label('last in')
# from matplotlib.alluvial import Alluvial from pyalluv import Alluvial ext = np.array([1, 3, 1]) flows = [[[0, 1, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0, 0], [1, 0, 1, 1]]] extouts = [[(1, (1, 1, 2), 1)], []] # Start with an empty alluvial plot alluv = Alluvial(x=['t1', 't2', 't3'], blockprops=dict(width=0.2)) # Add the first subdiagram alluv.add(flows=flows, ext=ext, extout=extouts, yoff=0, layout=['top', 'top', 'top']) # Adding a new subdiagram, using the same flows alluv.add(flows=flows, ext=ext, yoff=7, layout=['top', 'optimized', 'bottom']) alluv.finish() plt.show() ############################################################################# # # ------------ # # References # """""""""" # # The use of the following functions, methods, classes and modules is shown
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')