Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

silx.gui.plot.PlotWidget: Fixed display of Shape with dashes and a background color #3906

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 54 additions & 48 deletions src/silx/gui/plot/backends/BackendMatplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,42 +393,47 @@ def contains(self, mouseevent):
return self.line.contains(mouseevent)


class _DoubleColoredLinePatch(matplotlib.patches.Patch):
"""Matplotlib patch to display any patch using double color."""
class SecondEdgeColorPatchMixIn:
"""Mix-in class to add a second color for patches with dashed lines"""

def __init__(self, patch):
super(_DoubleColoredLinePatch, self).__init__()
self.__patch = patch
self.linebgcolor = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._second_edgecolor = None

def set_second_edgecolor(self, color):
"""Set the second color used to fill dashed edges"""
self._second_edgecolor = color

def __getattr__(self, name):
return getattr(self.__patch, name)
def get_second_edgecolor(self):
"""Returns the second color used to fill dashed edges"""
return self._second_edgecolor

def draw(self, renderer):
oldLineStype = self.__patch.get_linestyle()
if self.linebgcolor is not None and oldLineStype != "solid":
oldLineColor = self.__patch.get_edgecolor()
oldHatch = self.__patch.get_hatch()
self.__patch.set_linestyle("solid")
self.__patch.set_edgecolor(self.linebgcolor)
self.__patch.set_hatch(None)
self.__patch.draw(renderer)
self.__patch.set_linestyle(oldLineStype)
self.__patch.set_edgecolor(oldLineColor)
self.__patch.set_hatch(oldHatch)
self.__patch.draw(renderer)
linestyle = self.get_linestyle()
if linestyle == "solid" or self.get_second_edgecolor() is None:
super().draw(renderer)
return

edgecolor = self.get_edgecolor()
hatch = self.get_hatch()

self.set_linestyle("solid")
self.set_edgecolor(self.get_second_edgecolor())
self.set_hatch(None)
super().draw(renderer)

def set_transform(self, transform):
self.__patch.set_transform(transform)
self.set_linestyle(linestyle)
self.set_edgecolor(edgecolor)
self.set_hatch(hatch)
super().draw(renderer)
Comment on lines +417 to +428
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the same rendering as before, there's probably a better way, but this has been working fine so far.


def get_path(self):
return self.__patch.get_path()

def contains(self, mouseevent, radius=None):
return self.__patch.contains(mouseevent, radius)
class Rectangle2EdgeColor(SecondEdgeColorPatchMixIn, Rectangle):
"""Rectangle patch with a second edge color for dashed line"""

def contains_point(self, point, radius=None):
return self.__patch.contains_point(point, radius)

class Polygon2EdgeColor(SecondEdgeColorPatchMixIn, Polygon):
"""Polygon patch with a second edge color for dashed line"""


class Image(AxesImage):
Expand Down Expand Up @@ -805,19 +810,20 @@ def addShape(self, x, y, shape, color, fill, overlay,
yMax = numpy.nanmax(yView)
w = xMax - xMin
h = yMax - yMin
item = Rectangle(xy=(xMin, yMin),
width=w,
height=h,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth)
item = Rectangle2EdgeColor(
xy=(xMin, yMin),
width=w,
height=h,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth,
)
item.set_second_edgecolor(linebgcolor)

if fill:
item.set_hatch('.')

if linestyle != "solid" and linebgcolor is not None:
item = _DoubleColoredLinePatch(item)
item.linebgcolor = linebgcolor

self.ax.add_patch(item)

Expand All @@ -827,19 +833,19 @@ def addShape(self, x, y, shape, color, fill, overlay,
closed = True
else: # shape == 'polylines'
closed = numpy.all(numpy.equal(points[0], points[-1]))
item = Polygon(points,
closed=closed,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth)
item = Polygon2EdgeColor(
points,
closed=closed,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth,
)
item.set_second_edgecolor(linebgcolor)

if fill and shape == 'polygon':
item.set_hatch('/')

if linestyle != "solid" and linebgcolor is not None:
item = _DoubleColoredLinePatch(item)
item.linebgcolor = linebgcolor

self.ax.add_patch(item)

else:
Expand Down