Browse Source

When adding patterns to bars, reuse the same pattern for the same colors.

Carlos Garcia 6 years ago
parent
commit
96c9ea444d
1 changed files with 15 additions and 5 deletions
  1. 15 5
      paper_plots.py

+ 15 - 5
paper_plots.py

@@ -129,12 +129,22 @@ def setBarsBW(fig):
     patterns = ['0', '///', '---', '|||', '+++', '**', 'oo', '...']
     inx = 0
     boxes = []
+    # Associate each color with a pattern so as to reuse the patterns
+    color_associations = {}
+    # Find all rectangles in the graph
     for ax in fig.get_axes():
         for child in ax.get_children():
             if isinstance(child, matplotlib.patches.Rectangle):
                 boxes.append(child)
-    # Skip the last rectangle which should be the background
-    for box in boxes[:-1]:
-        box.set_hatch(patterns[inx])
-        inx += 1
-        if inx == len(patterns): inx = 0
+    # Skip some rectangles. Only change rectangles that have a color matching ours.
+    for box in boxes:
+        hex_color = matplotlib.colors.to_hex(box.get_facecolor())
+        if hex_color in paper_colors:
+            if hex_color not in color_associations:
+                color_associations[hex_color] = patterns[inx]
+                inx += 1
+                if inx == len(patterns): inx = 0
+
+            box.set_hatch(color_associations[hex_color])
+            
+