|
595 | 595 | "def add_counts_to_ticks(plot_data, xvar, yvar, rawdata_axes, plot_kwargs):\n", |
596 | 596 | " # Add the counts to the rawdata axes xticks.\n", |
597 | 597 | " counts = plot_data.groupby(xvar).count()[yvar]\n", |
598 | | - " ticks_with_counts = []\n", |
599 | | - " ticks_loc = rawdata_axes.get_xticks()\n", |
600 | | - " rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))\n", |
601 | | - " def lookup_value(text, counts):\n", |
| 598 | + " \n", |
| 599 | + " def lookup_value(text):\n", |
602 | 600 | " try:\n", |
603 | 601 | " return str(counts.loc[text])\n", |
604 | 602 | " except KeyError:\n", |
605 | 603 | " try:\n", |
606 | 604 | " numeric_key = pd.to_numeric(text, errors='coerce')\n", |
607 | 605 | " if pd.notnull(numeric_key):\n", |
608 | 606 | " return str(counts.loc[numeric_key])\n", |
609 | | - " else:\n", |
610 | | - " raise ValueError\n", |
611 | 607 | " except (ValueError, KeyError):\n", |
612 | | - " print(f\"Key '{text}' not found in counts.\")\n", |
613 | | - " return \"N/A\"\n", |
614 | | - " for xticklab in rawdata_axes.xaxis.get_ticklabels():\n", |
615 | | - " t = xticklab.get_text()\n", |
616 | | - " # Extract the text after the last newline, if present\n", |
617 | | - " if t.rfind(\"\\n\") != -1:\n", |
618 | | - " te = t[t.rfind(\"\\n\") + len(\"\\n\"):]\n", |
619 | | - " value = lookup_value(te, counts)\n", |
620 | | - " te = t\n", |
621 | | - " else:\n", |
622 | | - " te = t\n", |
623 | | - " value = lookup_value(te, counts)\n", |
624 | | - "\n", |
625 | | - " # Append the modified tick label with the count to the list\n", |
626 | | - " ticks_with_counts.append(f\"{te}\\nN = {value}\")\n", |
| 608 | + " pass\n", |
| 609 | + " print(f\"Key '{text}' not found in counts.\")\n", |
| 610 | + " return \"N/A\"\n", |
627 | 611 | "\n", |
| 612 | + " ticks_with_counts = []\n", |
| 613 | + " for xticklab in rawdata_axes.get_xticklabels():\n", |
| 614 | + " t = xticklab.get_text()\n", |
| 615 | + " te = t.split('\\n')[-1] # Get the last line of the label\n", |
| 616 | + " value = lookup_value(te)\n", |
| 617 | + " ticks_with_counts.append(f\"{t}\\nN = {value}\")\n", |
628 | 618 | "\n", |
629 | | - " if plot_kwargs[\"fontsize_rawxlabel\"] is not None:\n", |
630 | | - " fontsize_rawxlabel = plot_kwargs[\"fontsize_rawxlabel\"]\n", |
| 619 | + " fontsize_rawxlabel = plot_kwargs.get(\"fontsize_rawxlabel\")\n", |
631 | 620 | " rawdata_axes.set_xticklabels(ticks_with_counts, fontsize=fontsize_rawxlabel)\n", |
632 | 621 | "\n", |
| 622 | + " # Ensure ticks are at the correct locations\n", |
| 623 | + " rawdata_axes.xaxis.set_major_locator(plt.FixedLocator(rawdata_axes.get_xticks()))\n", |
| 624 | + "\n", |
633 | 625 | "\n", |
634 | 626 | "def extract_contrast_plotting_ticks(is_paired, show_pairs, two_col_sankey, plot_groups, idx, sankey_control_group):\n", |
635 | 627 | "\n", |
|
0 commit comments