Skip to content

Commit

Permalink
fix segmenter for mpl 3.7 (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhi authored Feb 28, 2023
1 parent 9d1bb0c commit 0807fa9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 24 deletions.
70 changes: 51 additions & 19 deletions docs/examples/image-segmentation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%matplotlib ipympl\n",
Expand All @@ -24,13 +26,18 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# load a sample image\n",
"image = plt.imread(\n",
" \"https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png\"\n",
")"
"import urllib\n",
"\n",
"import PIL\n",
"\n",
"url = \"https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png\"\n",
"image = np.array(PIL.Image.open(urllib.request.urlopen(url)))"
]
},
{
Expand Down Expand Up @@ -66,7 +73,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"segmenter.erasing = True"
Expand All @@ -93,7 +102,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"segmenter.mask"
Expand All @@ -102,7 +113,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"plt.figure()\n",
Expand All @@ -121,7 +134,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"multi_class_segmenter = image_segmenter(image, nclasses=3, mask_alpha=0.76)\n",
Expand All @@ -140,7 +155,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"multi_class_segmenter.current_class = 3"
Expand All @@ -149,7 +166,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"plt.figure()\n",
Expand All @@ -168,7 +187,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
Expand Down Expand Up @@ -240,7 +261,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from ipywidgets import HBox\n",
Expand All @@ -258,26 +281,35 @@
"source": [
"## LassoSelector line\n",
"\n",
"You can change the appearance of the LassoSelector line using the `lineprops` kwarg. So to make the line very thick and red:"
"You can change the appearance of the LassoSelector line using the `props` kwarg (or `lineprops` in matplotlib <3.7). So to make the line very thick and red:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"lineprops = {\"color\": \"red\", \"linewidth\": 10}\n",
"props = {\"color\": \"red\", \"linewidth\": 10}\n",
"gray = image_segmenter(\n",
" grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\", lineprops=lineprops\n",
" grayscale_image, nclasses=3, mask=mask, figsize=(5, 5), cmap=\"gray\", props=props\n",
")\n",
"display(gray)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -291,7 +323,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.9.9"
}
},
"nbformat": 4,
Expand Down
26 changes: 21 additions & 5 deletions mpl_interactions/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Callable

import numpy as np
from matplotlib import __version__ as mpl_version
from matplotlib import get_backend
from matplotlib.colors import TABLEAU_COLORS, XKCD_COLORS, to_rgba_array
from matplotlib.path import Path
Expand Down Expand Up @@ -429,6 +430,7 @@ def __init__(
mask_colors=None,
mask_alpha=0.75,
lineprops=None,
props=None,
lasso_mousebutton="left",
pan_mousebutton="middle",
ax=None,
Expand All @@ -452,8 +454,12 @@ def __init__(
The alpha values to use for selected regions. This will always override the alpha values
in mask_colors if any were passed
lineprops : dict, default: None
DEPRECATED - use props instead.
lineprops passed to LassoSelector. If None the default values are:
{"color": "black", "linewidth": 1, "alpha": 0.8}
props : dict, default: None
props passed to LassoSelector. If None the default values are:
{"color": "black", "linewidth": 1, "alpha": 0.8}
lasso_mousebutton : str, or int, default: "left"
The mouse button to use for drawing the selecting lasso.
pan_mousebutton : str, or int, default: "middle"
Expand Down Expand Up @@ -509,18 +515,28 @@ def __init__(
self.displayed = self.ax.imshow(self._img, **kwargs)
self._mask = self.ax.imshow(self._overlay)

if lineprops is None:
lineprops = {"color": "black", "linewidth": 1, "alpha": 0.8}
default_props = {"color": "black", "linewidth": 1, "alpha": 0.8}
if (props is None) and (lineprops is None):
props = default_props
elif (lineprops is not None) and (mpl_version >= "3.7"):
print("*lineprops* is deprecated - please use props")
props = {"color": "black", "linewidth": 1, "alpha": 0.8}

useblit = False if "ipympl" in get_backend().lower() else True
button_dict = {"left": 1, "middle": 2, "right": 3}
if isinstance(pan_mousebutton, str):
pan_mousebutton = button_dict[pan_mousebutton.lower()]
if isinstance(lasso_mousebutton, str):
lasso_mousebutton = button_dict[lasso_mousebutton.lower()]

self.lasso = LassoSelector(
self.ax, self._onselect, lineprops=lineprops, useblit=useblit, button=lasso_mousebutton
)
if mpl_version < "3.7":
self.lasso = LassoSelector(
self.ax, self._onselect, lineprops=props, useblit=useblit, button=lasso_mousebutton
)
else:
self.lasso = LassoSelector(
self.ax, self._onselect, props=props, useblit=useblit, button=lasso_mousebutton
)
self.lasso.set_visible(True)

pix_x = np.arange(self._img.shape[0])
Expand Down

0 comments on commit 0807fa9

Please sign in to comment.