Guest User

Untitled

a guest
Jun 30th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. # Success Criteria for this exercise is a subplot containing 2 Heatmaps side by side in the same Figure
  2. from pdb import set_trace
  3. from covidDash.plot_handlers.colorscales import bone_r
  4. from plotly.subplots import make_subplots
  5. import plotly.figure_factory as ff
  6. import plotly.graph_objects as go
  7.  
  8.  
  9. # DATA PREP SECTION
  10. # wow Heatmap data
  11. wow = {'x_labels' : {'name' : 'Week',
  12.                      'labels' : ['W21', 'W22', 'W23', 'W24', 'W25', 'W26']
  13.                     },
  14.        'y_labels' : {'name' : 'Site',
  15.                      'labels' : ['A', 'B', 'C', 'D', 'E', 'F', 'G']
  16.                     },
  17.        'data'     : [
  18.                       [0, 0, 1, 0, 0, 0],
  19.                       [0, 0, 3, 1, 0, 0],
  20.                       [0, 0, 0, 0, 0, 0],
  21.                       [0, 0, 0, 0, 0, 1],
  22.                       [0, 0, 0, 0, 0, 0],
  23.                       [0, 0, 1, 0, 0, 0],
  24.                       [0, 0, 0, 0, 0, 0]
  25.                     ],
  26.        'data_labels' : []
  27.       }
  28.  
  29.  
  30. # 6w and YTD Heatmap data
  31. totals = {'x_labels' : {'name' : 'Week',
  32.                         'labels' :['6W', 'YTD' ]
  33.          },
  34.           'y_labels' : wow['y_labels'],
  35.           'data'     : [
  36.                          [1, 16],
  37.                          [4, 8],
  38.                          [0, 1],
  39.                          [1, 12],
  40.                          [0, 5],
  41.                          [1, 17],
  42.                          [0, 1]
  43.                       ],
  44.          'data_labels' : []
  45.          }
  46.  
  47.  
  48. # this function is simply a base func for now
  49. def int_to_str(arr2d):
  50.   """base function for handling data to label conversion
  51.  Args:
  52.  
  53.    arr2d (list): a 2D array with numeric values
  54.  
  55.  Returns:
  56.  
  57.    r_data (list): a 2D array with values converted into strings
  58.  
  59.  """
  60.   r_data = []
  61.   for row in arr2d:
  62.     new_row = []
  63.     [new_row.append(str(n)) for n in row]
  64.     r_data.append(new_row)
  65.   return r_data
  66.  
  67.  
  68. wow['data_labels'] = int_to_str(wow['data'])
  69. totals['data_labels'] = int_to_str(totals['data'])
  70.  
  71. # PLOT PREP SECTION
  72. # colorbar placement
  73. wow_cbar= {
  74.       'x' : 1.0,
  75.       'title' : {
  76.         'text' : 'Wow',
  77.         'side' : 'right'
  78.       }
  79.     }
  80.  
  81.  
  82. total_cbar = {
  83.         'x' : 1.05,
  84.         'title' : {
  85.           'text' : 'Totals',
  86.           'side' : 'right'
  87.          }
  88.        }
  89.  
  90. # xaxis conf
  91. xaxis_conf={'rangeslider': {'visible': True},
  92.        'type' : 'category',
  93.        'side' : 'top'
  94.       }
  95.  
  96.  
  97. # week over week figure
  98. fig1 = ff.create_annotated_heatmap(x=wow['x_labels']['labels'],
  99.                                   y=wow['y_labels']['labels'],
  100.                                   z=wow['data'],
  101.                                   colorscale=bone_r,
  102.                                   font_colors=['black','white'],
  103.                                   showscale=True,
  104.                                   annotation_text=wow['data_labels'],
  105.                                   colorbar=wow_cbar,
  106.                                   name='Wow'
  107.                                   )
  108.  
  109.  
  110. # 6W and YTD
  111. fig2 =ff.create_annotated_heatmap(x=totals['x_labels']['labels'],
  112.                                   y=totals['y_labels']['labels'],
  113.                                   z=totals['data'],
  114.                                   colorscale=bone_r,
  115.                                   font_colors=['black','white'],
  116.                                   showscale=True,
  117.                                   annotation_text=totals['data_labels'],
  118.                                   colorbar=total_cbar,
  119.                                   name='Totals',
  120.                                   )
  121. # SUBPLOT PREP SECTION
  122. # base subplot
  123. fig = make_subplots(
  124.     rows=1, cols=2,
  125.     shared_yaxes=True,
  126.     horizontal_spacing=0,
  127.     row_titles=wow['y_labels']['labels'],
  128. #    y_title=[wow['y_labels']['name'],
  129.     x_title=wow['x_labels']['name'],
  130.     column_widths=[0.75, 0.25]
  131. )
  132.  
  133. # add data
  134. fig.add_trace(fig1.data[0], 1, 1)
  135. fig.add_trace(fig2.data[0], 1, 2)
  136.  
  137. # apply annotations
  138. wow_annot = list(fig1.layout.annotations)
  139. totals_annot = list(fig2.layout.annotations)
  140. for k in range(len(totals_annot)):
  141.   totals_annot[k]['xref'] = 'x2'
  142.   totals_annot[k]['yref'] = 'y2'
  143.  
  144. fig.update_layout(annotations=wow_annot+totals_annot,xaxis=xaxis_conf, xaxis2={'side':'top'})
  145. set_trace()
  146.  
  147. fig.show()
Advertisement
Add Comment
Please, Sign In to add comment