Skip to content

Commit ff3a06f

Browse files
committed
Updated swarmplot to produce legends if plotting by hue column
1 parent 319f513 commit ff3a06f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

dabest/plot_tools.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ def plot(
13051305
0 # x-coordinate of center of each individual swarm of the swarm plot
13061306
)
13071307
x_tick_tabels = []
1308+
13081309
for group_i, values_i in self.__data_copy.groupby(self.__x):
13091310
x_new = []
13101311
values_i_y = values_i[self.__y]
@@ -1363,16 +1364,31 @@ def plot(
13631364
)
13641365
else:
13651366
facecolor = "none" if not filled else self.__palette[group_i]
1367+
13661368
ax.scatter(
13671369
values_i["x_new"],
13681370
values_i[self.__y],
13691371
s=self.__size,
13701372
zorder=self.__zorder,
13711373
facecolor=facecolor,
13721374
edgecolor=self.__palette[group_i],
1375+
label=group_i,
13731376
**kwargs,
13741377
)
13751378

1379+
# Handling of legends
1380+
# This is currently a workaround because c and cmap is unable to map the labels when calling scatter()
1381+
# labels has to be used to designate legend labels and handles in scatter() due to the potential calling of ax.get_legend_handles_labels()
1382+
if self.__hue is not None:
1383+
for cmap_group_i in self.__palette:
1384+
ax.scatter(
1385+
[],
1386+
[],
1387+
c=self.__palette[cmap_group_i],
1388+
label=cmap_group_i,
1389+
)
1390+
handles, labels = ax.get_legend_handles_labels()
1391+
13761392
ax.get_xaxis().set_ticks(np.arange(x_position))
13771393
ax.get_xaxis().set_ticklabels(x_tick_tabels)
13781394

nbs/API/plot_tools.ipynb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@
13681368
" 0 # x-coordinate of center of each individual swarm of the swarm plot\n",
13691369
" )\n",
13701370
" x_tick_tabels = []\n",
1371+
"\n",
13711372
" for group_i, values_i in self.__data_copy.groupby(self.__x):\n",
13721373
" x_new = []\n",
13731374
" values_i_y = values_i[self.__y]\n",
@@ -1426,16 +1427,31 @@
14261427
" )\n",
14271428
" else:\n",
14281429
" facecolor = \"none\" if not filled else self.__palette[group_i]\n",
1430+
"\n",
14291431
" ax.scatter(\n",
14301432
" values_i[\"x_new\"],\n",
14311433
" values_i[self.__y],\n",
14321434
" s=self.__size,\n",
14331435
" zorder=self.__zorder,\n",
14341436
" facecolor=facecolor,\n",
14351437
" edgecolor=self.__palette[group_i],\n",
1438+
" label=group_i,\n",
14361439
" **kwargs,\n",
14371440
" )\n",
14381441
"\n",
1442+
" # Handling of legends\n",
1443+
" # This is currently a workaround because c and cmap is unable to map the labels when calling scatter()\n",
1444+
" # labels has to be used to designate legend labels and handles in scatter() due to the potential calling of ax.get_legend_handles_labels()\n",
1445+
" if self.__hue is not None:\n",
1446+
" for cmap_group_i in self.__palette:\n",
1447+
" ax.scatter(\n",
1448+
" [],\n",
1449+
" [],\n",
1450+
" c=self.__palette[cmap_group_i],\n",
1451+
" label=cmap_group_i,\n",
1452+
" )\n",
1453+
" handles, labels = ax.get_legend_handles_labels()\n",
1454+
"\n",
14391455
" ax.get_xaxis().set_ticks(np.arange(x_position))\n",
14401456
" ax.get_xaxis().set_ticklabels(x_tick_tabels)\n",
14411457
"\n",

0 commit comments

Comments
 (0)