def test_default_linestyle(): patch = Patch() patch.set_linestyle('--') patch.set_linestyle(None) assert patch.get_linestyle() == 'solid'
def apply_style(style: dict, patch: mpatches.Patch) -> dict: """ apply the properties defined in style to the given patch """ fill_opacity = float(style.get("opacity", 1)) * float(style.get("fill-opacity", 1)) stroke_opacity = float(style.get("opacity", 1)) * float(style.get("stroke-opacity", 1)) def readColor(value): try: return mcolors.to_rgb(value) except: # matplotlib cannot handle html colors in the form #000 if len(value) == 4 and value[0] == "#": return readColor("#"+value[1]*2+value[2]*2+value[3]*2) raise # matplotlib defaults differ if "fill" not in style: style["fill"] = "none" if "stroke" not in style: style["stroke"] = "none" for key, value in style.items(): try: if key == "opacity": pass #patch.set_alpha(float(value)) elif key == "fill": if value == "none" or value == "transparent": patch.set_facecolor("none") else: try: r, g, b = readColor(value) patch.set_facecolor((r, g, b, fill_opacity)) except Exception as err: patch.set_facecolor("none") raise elif key == "fill-opacity": pass elif key == "stroke": if value == "none" or value == "transparent": patch.set_edgecolor("none") else: try: r, g, b = readColor(value) patch.set_edgecolor((r, g, b, stroke_opacity)) except Exception as err: patch.set_edgecolor("none") raise elif key == "stroke-opacity": pass elif key == "stroke-dasharray": if value != "none": offset = 0 if isinstance(patch.get_linestyle(), tuple): offset, dashes = patch.get_linestyle() patch.set_linestyle((offset, [float(s)*4 for s in value.split(",")])) elif key == "stroke-dashoffset": dashes = [1, 0] if isinstance(patch.get_linestyle(), tuple): offset, dashes = patch.get_linestyle() patch.set_linestyle((float(value)*4, dashes)) elif key == "stroke-linecap": if value == "square": value = "projecting" patch.set_capstyle(value) elif key == "stroke-linejoin": patch.set_joinstyle(value) elif key == "stroke-miterlimit": pass # unfortunately we cannot implement this in matplotlib elif key == "stroke-width": try: patch.set_linewidth(svgUnitToMpl(value)) except: pass elif key == "stroke-linecap": try: patch.set_dash_capstyle(value) patch.set_solid_capstyle(value) except AttributeError: pass elif key == "font-size": pass elif key == "font-weight": pass elif key == "font-style": pass elif key == "font-family": pass elif key == "font-variant": pass elif key == "font-stretch": pass elif key == "display": pass elif key == "text-anchor": pass else: print("ERROR: unknown style key", key, file=sys.stderr) except ValueError: print("ERROR: could not set style", key, value, file=sys.stderr) return style