|
3 | 3 | """
|
4 | 4 | import seaborn as sns
|
5 | 5 | import matplotlib.patches as mpatches
|
6 |
| -import matplotlib.pyplot as plt |
7 | 6 | import matplotlib.ticker as mticker
|
8 | 7 | import matplotlib as mpl
|
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import numpy as np |
9 | 10 |
|
10 | 11 |
|
11 | 12 | # Helper functions
|
@@ -177,15 +178,53 @@ def adjust_spine_to_middle(ax=None):
|
177 | 178 | return ax
|
178 | 179 |
|
179 | 180 |
|
180 |
| -def x_ticks_0(ax): |
| 181 | +def ticks_0(ax, show_zero=True, axis="x", precision=2): |
181 | 182 | """Apply custom formatting for x-axis ticks."""
|
182 |
| - def custom_x_ticks(x, pos): |
| 183 | + if axis not in ["x", "y"]: |
| 184 | + raise ValueError("'axis' should be 'x' or 'y'") |
| 185 | + def custom_ticks(x, pos): |
183 | 186 | """Format x-axis ticks."""
|
184 |
| - if x % 1 == 0: # Check if number is an integer |
| 187 | + if x == 0 and not show_zero: |
| 188 | + return '' |
| 189 | + elif x % 1 == 0: # Check if number is an integer |
| 190 | + return f'{int(x)}' # Format as integer |
| 191 | + else: |
| 192 | + # Format as float with two decimal places |
| 193 | + return f'{x:.{precision}f}' |
| 194 | + if axis == "x": |
| 195 | + ax.xaxis.set_major_formatter(mticker.FuncFormatter(custom_ticks)) |
| 196 | + else: |
| 197 | + ax.yaxis.set_major_formatter(mticker.FuncFormatter(custom_ticks)) |
| 198 | + |
| 199 | + |
| 200 | +def ticks_0(ax, show_zero=True, show_only_max=False, axis="x", precision=2): |
| 201 | + """Apply custom formatting for axis ticks and ensure max value is shown.""" |
| 202 | + if axis not in ["x", "y"]: |
| 203 | + raise ValueError("'axis' should be 'x' or 'y'") |
| 204 | + # Format tick labels |
| 205 | + def custom_ticks(x, pos): |
| 206 | + """Format axis ticks.""" |
| 207 | + if x == 0 and not show_zero: |
| 208 | + return '' |
| 209 | + elif x % 1 == 0: # Check if number is an integer |
185 | 210 | return f'{int(x)}' # Format as integer
|
186 | 211 | else:
|
187 |
| - return f'{x:.2f}' # Format as float with two decimal places |
188 |
| - ax.xaxis.set_major_formatter(mticker.FuncFormatter(custom_x_ticks)) |
| 212 | + # Format as float with specified precision |
| 213 | + return f'{x:.{precision}f}' |
| 214 | + |
| 215 | + # Get the current axis object |
| 216 | + axis_obj = ax.xaxis if axis == "x" else ax.yaxis |
| 217 | + axis_obj.set_major_formatter(mticker.FuncFormatter(custom_ticks)) |
| 218 | + |
| 219 | + # Get the current limits |
| 220 | + if show_only_max: |
| 221 | + vmax = ax.get_xlim()[1] if axis == "x" else ax.get_ylim()[1] |
| 222 | + max_val = int(np.ceil(vmax)) |
| 223 | + new_ticks = [max_val] |
| 224 | + if axis == "x": |
| 225 | + ax.set_xticks(new_ticks) |
| 226 | + else: |
| 227 | + ax.set_yticks(new_ticks) |
189 | 228 |
|
190 | 229 |
|
191 | 230 | def adjust_tuple_elements(tuple_in=None, tuple_default=None):
|
|
0 commit comments