code: def add_segmented_colorbar(da, colors, direction):
      
      nbreak = len(colors)
      if direction == 'vertical':
          linewidth = da.height/nbreak
          verts = [None] * nbreak
          x1, x2 = 0, da.width
          for i, color in enumerate(colors):
              y1 = i * linewidth
              y2 = y1 + linewidth
              verts[i] = ((x1, y1), (x1, y2), (x2, y2), (x2, y1))
      else:
          linewidth = da.width/nbreak
          verts = [None] * nbreak
          y1, y2 = 0, da.height
          for i, color in enumerate(colors):
              x1 = i * linewidth
              x2 = x1 + linewidth
              verts[i] = ((x1, y1), (x1, y2), (x2, y2), (x2, y1))
  
      coll = mcoll.PolyCollection(verts,
                                  facecolors=colors,
                                  linewidth=0,
                                  antialiased=False)
      da.add_artist(coll)
docs: Add 'non-rastered' colorbar to DrawingArea