Skip to content

Commit

Permalink
Merge pull request #55 from haesleinhuepf/markersize
Browse files Browse the repository at this point in the history
Make markersize in plots configurable
  • Loading branch information
haesleinhuepf authored Jul 15, 2024
2 parents 6594a1f + 68132ff commit 2db6949
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
55 changes: 52 additions & 3 deletions docs/scatterplot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "33e97c513c2446b887d8063b0860293d",
"model_id": "90f8e79f06174f038601ab84bbba8f64",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -323,7 +323,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1bc5c7a22b944f82859f5e643fed62cb",
"model_id": "98ab4d4f5a1f41329a2d7d039cf57d52",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -386,10 +386,59 @@
"df[\"selection\"]"
]
},
{
"cell_type": "markdown",
"id": "8c418529-5596-415a-ac1f-7485da7790e0",
"metadata": {},
"source": [
"## Custom widgets\n",
"You can also build such a side-by-side view with two plots yourself:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d8f85ddd-160e-44f7-a5e3-eb42e032f093",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6364820096204b41969d4d12eeabd489",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(HBox(children=(VBox(children=(VBox(children=(HBox(children=(Label(value='Axes '), Dropdown(layo…"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import ipywidgets as widgets\n",
"from ipywidgets import HBox\n",
"\n",
"# Program functions that are executed when the selection is changed.\n",
"def update2(e=None):\n",
" widget2.update()\n",
"\n",
"def update1(e=None):\n",
" widget1.update()\n",
" \n",
"widget1 = stackview.scatterplot(df, column_x=\"centroid-0\", column_y=\"centroid-1\", selection_changed_callback=update2, markersize=50)\n",
"widget2 = stackview.scatterplot(df, column_x=\"area\", column_y=\"aspect_ratio\", selection_changed_callback=update1)\n",
"\n",
"# Arrange the widgets side by side using HBox\n",
"HBox([widget1, widget2])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "405b6843-28dd-40dd-a22d-925c0b34040b",
"id": "e54890a5-e89c-4861-bf5b-8aa1c5f7f697",
"metadata": {},
"outputs": [],
"source": []
Expand Down
8 changes: 5 additions & 3 deletions stackview/_clusterplot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def clusterplot(df, labels, column_x:str="x", column_y:str="y", column_selection:str="selection", image=None,
zoom_factor:float=1, zoom_spline_order:int=0, figsize=(4, 4), alpha:float=0.5):
zoom_factor:float=1, zoom_spline_order:int=0, figsize=(4, 4), alpha:float=0.5, markersize=4):
"""
Visualizes a scatter plot of columns in a given dataframe next to a segmented image.
Expand Down Expand Up @@ -28,7 +28,9 @@ def clusterplot(df, labels, column_x:str="x", column_y:str="y", column_selection
The size of the scatter plot figure
alpha: float, optional
sets the transparency of the curtain
markersize: int
The size of the markers
Returns
-------
An ipywidgets widget
Expand Down Expand Up @@ -66,7 +68,7 @@ def update(selection, label_image, selected_image, widget):
widget=image_display)

scatterplot = scatterplot(df, column_x, column_y, column_selection, figsize=figsize,
selection_changed_callback=update_selection)
selection_changed_callback=update_selection, markersize=markersize)

return grid([[
image_display,
Expand Down
19 changes: 13 additions & 6 deletions stackview/_scatterplot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

def scatterplot(df, column_x: str = "x", column_y: str = "y", column_selection: str = "selection", figsize=(4, 4), selection_changed_callback=None):
def scatterplot(df, column_x: str = "x", column_y: str = "y", column_selection: str = "selection", figsize=(4, 4), selection_changed_callback=None, markersize:int=4):
"""
Create a scatterplot of a pandas dataframe while interactively choosing the columns and using a lasso tool for selecting data points
Expand All @@ -18,6 +18,8 @@ def scatterplot(df, column_x: str = "x", column_y: str = "y", column_selection:
The size of the scatter plot figure
selection_changed_callback: function
The function to call when the selection changes
markersize: int
The size of the markers
Returns
-------
Expand All @@ -27,7 +29,7 @@ def scatterplot(df, column_x: str = "x", column_y: str = "y", column_selection:
from ipywidgets import VBox, HBox, Layout
from ._utilities import _no_resize

plotter = ScatterPlotter(df, column_x, column_y, column_selection, figsize, selection_changed_callback=selection_changed_callback)
plotter = ScatterPlotter(df, column_x, column_y, column_selection, figsize, selection_changed_callback=selection_changed_callback, markersize=markersize)
small_layout = Layout(width='auto', padding='0px', margin='0px', align_items='center', justify_content='center')

import ipywidgets
Expand Down Expand Up @@ -56,11 +58,13 @@ def on_change(event):
plotter.widget
]))

result.update = plotter.update

return result


class ScatterPlotter():
def __init__(self, df, column_x, column_y, column_selection, figsize, selection_changed_callback):
def __init__(self, df, column_x, column_y, column_selection, figsize, selection_changed_callback, markersize):
"""
An interactive scatter plotter for pandas dataframes.
Use `.widget` on this object to get access to the graphical user interface.
Expand All @@ -79,6 +83,8 @@ def __init__(self, df, column_x, column_y, column_selection, figsize, selection_
The size of the figure
selection_changed_callback: function
The function to call when the selection changes
markersize: int
The size of the markers
"""
import matplotlib.pyplot as plt
from matplotlib._pylab_helpers import Gcf
Expand All @@ -94,10 +100,11 @@ def __init__(self, df, column_x, column_y, column_selection, figsize, selection_
self.set_data(df, column_x, column_y)
self.selection_column = column_selection
self.selection_changed_callback = selection_changed_callback
self.markersize = markersize

# create figure
self.fig = plt.figure(figsize=figsize)
self.fig.tight_layout(pad=0, h_pad=0, w_pad=0)
#self.fig.tight_layout(pad=0, h_pad=0, w_pad=0)
plt.subplots_adjust(left=0.15, right=1, top=1, bottom=0.1)

self.ax = None
Expand Down Expand Up @@ -139,7 +146,7 @@ def set_selection(self, selection):
def update(self):
self.fig.clf()
self.ax = self.fig.gca()
self.plotted_points = self.ax.scatter(self.data[0], self.data[1])
self.plotted_points = self.ax.scatter(self.data[0], self.data[1], s=self.markersize)
self.ax.set_xlabel(self.column_x)
self.ax.set_ylabel(self.column_y)
self.selector = Selector(self.fig, self.ax, self.plotted_points, callback=self.set_selection)
Expand All @@ -160,7 +167,7 @@ def __init__(self, parent, ax, collection, callback):
self.num_points = len(self.offsets)
self.collection = collection

self.lasso = LassoSelector(ax, onselect=self.on_select)
self.lasso = LassoSelector(ax, onselect=self.on_select, props=dict(color='magenta'))
self.selected_indices = []
self.callback = callback

Expand Down

0 comments on commit 2db6949

Please sign in to comment.