|
866 | 866 | " rawdata_axes.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks_loc))\n", |
867 | 867 | " for xticklab in rawdata_axes.xaxis.get_ticklabels():\n", |
868 | 868 | " t = xticklab.get_text()\n", |
869 | | - " if t.rfind(\"\\n\") != -1:\n", |
870 | | - " te = t[t.rfind(\"\\n\") + len(\"\\n\") :]\n", |
871 | | - " N = str(counts.loc[te])\n", |
872 | | - " te = t\n", |
873 | | - " else:\n", |
874 | | - " te = t\n", |
| 869 | + " # Extract the text after the last newline, if present\n", |
| 870 | + " te = t[t.rfind(\"\\n\") + len(\"\\n\"):] if t.rfind(\"\\n\") != -1 else t\n", |
| 871 | + "\n", |
| 872 | + " try:\n", |
| 873 | + " # Try to access 'counts' directly with 'te'.\n", |
875 | 874 | " N = str(counts.loc[te])\n", |
| 875 | + " except KeyError:\n", |
| 876 | + " # If direct access fails, attempt a numeric interpretation.\n", |
| 877 | + " try:\n", |
| 878 | + " # Attempt to convert 'te' to numeric (float or int, as appropriate)\n", |
| 879 | + " numeric_key = pd.to_numeric(te, errors='coerce')\n", |
| 880 | + " # 'pd.to_numeric()' will convert strings to float or int, as appropriate,\n", |
| 881 | + " # and will return NaN if conversion fails. It preserves integers.\n", |
| 882 | + " if pd.notnull(numeric_key): # Check if conversion was successful\n", |
| 883 | + " N = str(counts.loc[numeric_key])\n", |
| 884 | + " else:\n", |
| 885 | + " raise ValueError # Raise an error to trigger the except block\n", |
| 886 | + " except (ValueError, KeyError):\n", |
| 887 | + " # Handle cases where 'te' cannot be converted or the converted key doesn't exist\n", |
| 888 | + " print(f\"Key '{te}' not found in counts.\")\n", |
| 889 | + " N = \"N/A\"\n", |
| 890 | + "\n", |
| 891 | + " # Append the modified tick label with the count to the list\n", |
| 892 | + " ticks_with_counts.append(f\"{te}\\nN = {N}\")\n", |
876 | 893 | "\n", |
877 | | - " ticks_with_counts.append(\"{}\\nN = {}\".format(te, N))\n", |
878 | 894 | "\n", |
879 | 895 | " if plot_kwargs[\"fontsize_rawxlabel\"] is not None:\n", |
880 | 896 | " fontsize_rawxlabel = plot_kwargs[\"fontsize_rawxlabel\"]\n", |
|
0 commit comments