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_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')