|
62 | 62 | "import matplotlib.pyplot as plt\n",
|
63 | 63 | "# %matplotlib inline\n",
|
64 | 64 | "import seaborn as sns\n",
|
65 |
| - "from typing import List, Optional, Union\n" |
| 65 | + "from typing import List, Optional, Union\n", |
| 66 | + "import numpy as np" |
66 | 67 | ]
|
67 | 68 | },
|
68 | 69 | {
|
|
324 | 325 | " horizontal,\n",
|
325 | 326 | " marker_kwargs,\n",
|
326 | 327 | " errorbar_kwargs,\n",
|
| 328 | + " delta_text_kwargs,\n", |
327 | 329 | " marker_size\n",
|
328 | 330 | " ):\n",
|
329 | 331 | " from .misc_tools import merge_two_dicts\n",
|
|
376 | 378 | " else:\n",
|
377 | 379 | " errorbar_kwargs = merge_two_dicts(default_errorbar_kwargs, errorbar_kwargs)\n",
|
378 | 380 | "\n",
|
379 |
| - " return violin_kwargs, zeroline_kwargs, marker_kwargs, errorbar_kwargs\n", |
| 381 | + "\n", |
| 382 | + " # Delta text kwargs\n", |
| 383 | + " default_delta_text_kwargs = {\n", |
| 384 | + " \"color\": None, \n", |
| 385 | + " \"alpha\": 1,\n", |
| 386 | + " \"fontsize\": 10, \n", |
| 387 | + " \"ha\": 'center', \n", |
| 388 | + " \"va\": 'center', \n", |
| 389 | + " \"rotation\": 0, \n", |
| 390 | + " \"x_coordinates\": None, \n", |
| 391 | + " \"y_coordinates\": None,\n", |
| 392 | + " \"offset\": 0\n", |
| 393 | + " }\n", |
| 394 | + " if delta_text_kwargs is None:\n", |
| 395 | + " delta_text_kwargs = default_delta_text_kwargs\n", |
| 396 | + " else:\n", |
| 397 | + " delta_text_kwargs = merge_two_dicts(default_delta_text_kwargs, delta_text_kwargs)\n", |
| 398 | + "\n", |
| 399 | + " return violin_kwargs, zeroline_kwargs, marker_kwargs, errorbar_kwargs, delta_text_kwargs\n", |
380 | 400 | "\n",
|
381 | 401 | "\n",
|
382 | 402 | "def color_palette(\n",
|
|
430 | 450 | " yticklabels: Optional[list[str]] = None,\n",
|
431 | 451 | " remove_spines: bool = True,\n",
|
432 | 452 | "\n",
|
| 453 | + " delta_text: bool = True,\n", |
| 454 | + " delta_text_kwargs: dict = None,\n", |
| 455 | + "\n", |
433 | 456 | " violin_kwargs: Optional[dict] = None,\n",
|
434 | 457 | " zeroline_kwargs: Optional[dict] = None,\n",
|
435 | 458 | " marker_kwargs: Optional[dict] = None,\n",
|
|
485 | 508 | " Custom y-tick labels for the plot.\n",
|
486 | 509 | " remove_spines : bool, default=True\n",
|
487 | 510 | " If True, removes plot spines (except the relevant dependent variable spine).\n",
|
| 511 | + "\n", |
| 512 | + "\n", |
| 513 | + "\n", |
488 | 514 | " violin_kwargs : Optional[dict], default=None\n",
|
489 | 515 | " Additional arguments for violin plot customization.\n",
|
490 | 516 | " zeroline_kwargs : Optional[dict], default=None\n",
|
|
546 | 572 | " fig, ax = plt.subplots(figsize=fig_size)\n",
|
547 | 573 | "\n",
|
548 | 574 | " # Get Kwargs\n",
|
549 |
| - " violin_kwargs, zeroline_kwargs, marker_kwargs, errorbar_kwargs = get_kwargs(\n", |
550 |
| - " violin_kwargs = violin_kwargs,\n", |
551 |
| - " zeroline_kwargs = zeroline_kwargs,\n", |
552 |
| - " horizontal = horizontal,\n", |
553 |
| - " marker_kwargs = marker_kwargs,\n", |
554 |
| - " errorbar_kwargs = errorbar_kwargs,\n", |
555 |
| - " marker_size = marker_size\n", |
| 575 | + " (violin_kwargs, zeroline_kwargs, \n", |
| 576 | + " marker_kwargs, errorbar_kwargs, delta_text_kwargs) = get_kwargs(\n", |
| 577 | + " violin_kwargs = violin_kwargs,\n", |
| 578 | + " zeroline_kwargs = zeroline_kwargs,\n", |
| 579 | + " horizontal = horizontal,\n", |
| 580 | + " marker_kwargs = marker_kwargs,\n", |
| 581 | + " errorbar_kwargs = errorbar_kwargs,\n", |
| 582 | + " delta_text_kwargs = delta_text_kwargs,\n", |
| 583 | + " marker_size = marker_size\n", |
556 | 584 | " )\n",
|
557 | 585 | " \n",
|
558 | 586 | " # Plot the violins and make adjustments\n",
|
|
651 | 679 | " spines = [\"top\", \"right\", \"left\"] if horizontal else [\"top\", \"right\", \"bottom\"]\n",
|
652 | 680 | " ax.spines[spines].set_visible(False)\n",
|
653 | 681 | "\n",
|
| 682 | + " # Delta Text\n", |
| 683 | + " if delta_text:\n", |
| 684 | + " if delta_text_kwargs.get('color') is not None:\n", |
| 685 | + " delta_text_colors = [delta_text_kwargs.pop('color')] * number_of_curves_to_plot\n", |
| 686 | + " else:\n", |
| 687 | + " delta_text_colors = violin_colors\n", |
| 688 | + " delta_text_kwargs.pop('color')\n", |
| 689 | + "\n", |
| 690 | + " # Collect the X-coordinates for the delta text\n", |
| 691 | + " delta_text_x_coordinates = delta_text_kwargs.pop('x_coordinates')\n", |
| 692 | + " delta_text_x_adjustment = delta_text_kwargs.pop('offset')\n", |
| 693 | + "\n", |
| 694 | + " if delta_text_x_coordinates is not None:\n", |
| 695 | + " if not isinstance(delta_text_x_coordinates, (list, tuple)) or not all(isinstance(x, (int, float)) for x in delta_text_x_coordinates):\n", |
| 696 | + " raise TypeError(\"delta_text_kwargs['x_coordinates'] must be a list of x-coordinates.\")\n", |
| 697 | + " if len(delta_text_x_coordinates) != number_of_curves_to_plot:\n", |
| 698 | + " raise ValueError(\"delta_text_kwargs['x_coordinates'] must have the same length as the number of ticks to plot.\")\n", |
| 699 | + " else:\n", |
| 700 | + " delta_text_x_coordinates = (np.arange(1, number_of_curves_to_plot + 1) \n", |
| 701 | + " + (0.5 if not horizontal else -0.4)\n", |
| 702 | + " + delta_text_x_adjustment\n", |
| 703 | + " )\n", |
| 704 | + "\n", |
| 705 | + " # Collect the Y-coordinates for the delta text\n", |
| 706 | + " delta_text_y_coordinates = delta_text_kwargs.pop('y_coordinates')\n", |
| 707 | + "\n", |
| 708 | + " if delta_text_y_coordinates is not None:\n", |
| 709 | + " if not isinstance(delta_text_y_coordinates, (list, tuple)) or not all(isinstance(y, (int, float)) for y in delta_text_y_coordinates):\n", |
| 710 | + " raise TypeError(\"delta_text_kwargs['y_coordinates'] must be a list of y-coordinates.\")\n", |
| 711 | + " if len(delta_text_y_coordinates) != number_of_curves_to_plot:\n", |
| 712 | + " raise ValueError(\"delta_text_kwargs['y_coordinates'] must have the same length as the number of ticks to plot.\")\n", |
| 713 | + " else:\n", |
| 714 | + " delta_text_y_coordinates = differences\n", |
| 715 | + "\n", |
| 716 | + " if horizontal:\n", |
| 717 | + " delta_text_x_coordinates, delta_text_y_coordinates = delta_text_y_coordinates, delta_text_x_coordinates\n", |
| 718 | + "\n", |
| 719 | + " for idx, x, y, delta in zip(np.arange(0, number_of_curves_to_plot, 1), delta_text_x_coordinates, \n", |
| 720 | + " delta_text_y_coordinates, differences):\n", |
| 721 | + " delta_text = np.format_float_positional(delta, precision=2, sign=True, trim=\"k\", min_digits=2)\n", |
| 722 | + " ax.text(x, y, delta_text, color=delta_text_colors[idx], zorder=5, **delta_text_kwargs)\n", |
| 723 | + "\n", |
654 | 724 | " ## Invert Y-axis if horizontal \n",
|
655 | 725 | " if horizontal:\n",
|
656 | 726 | " ax.invert_yaxis()\n",
|
|
0 commit comments