Skip to content

Commit bae734d

Browse files
authored
syntax updates
1 parent b0ec995 commit bae734d

1 file changed

Lines changed: 16 additions & 27 deletions

File tree

fishplotpy/plot.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ def _map_pos_to_ha_va(pos: int) -> Tuple[str, str]:
3131
return 'left', 'center'
3232

3333

34-
def _annot_clone(ax: plt.Axes, x: float, y: float, annot: str,
35-
angle: float, col: str, pos: int, cex: float, offset: float, use_outline: bool = False):
34+
def _annot_clone(ax: plt.Axes, x: float, y: float, annot: str, angle: float, col: str, pos: int, cex: float, offset: float, use_outline: bool = False):
3635
"""Adds annotation text to the plot."""
37-
if not annot: # Skip if annotation is empty
36+
if not annot:
3837
return
3938

4039
ha, va = _map_pos_to_ha_va(pos)
@@ -46,7 +45,7 @@ def _annot_clone(ax: plt.Axes, x: float, y: float, annot: str,
4645
x_range = ax.get_xlim()[1] - ax.get_xlim()[0]
4746
y_range = ax.get_ylim()[1] - ax.get_ylim()[0]
4847
offset_scale_x = x_range * 0.01 # Scale offset relative to x-axis range
49-
offset_scale_y = y_range * 0.01 # Scale offset relative to y-axis range
48+
offset_scale_y = y_range * 0.01 # and y-axis
5049

5150
x_off, y_off = 0.0, 0.0
5251
if pos == 1: # Below -> offset pushes down
@@ -82,7 +81,7 @@ def _draw_clust_polygon(ax: plt.Axes, xpos: np.ndarray, ytop: np.ndarray, ybtm:
8281
annot_data: Dict[str, Any], clone_idx: int, fish_data: FishPlotData, use_outline: bool = False):
8382
"""Draws a single clone using polygons."""
8483
if len(xpos) == 0:
85-
return # Nothing to draw
84+
return # nothing to draw
8685

8786
# Calculate origin
8887
xst = xpos[0] - pad_left * (0.6 ** nest_level)
@@ -125,7 +124,6 @@ def _draw_clust_bezier(ax: plt.Axes, xpos: np.ndarray, ytop: np.ndarray, ybtm: n
125124
return
126125

127126
# R implementation Hmisc::bezier adds 'flank' points.
128-
# Let's replicate this logic by adding extra points around the originals.
129127
if len(xpos) > 1:
130128
x_range = np.max(xpos) - np.min(xpos)
131129
flank = x_range * 0.01 # R's flank calculation
@@ -150,12 +148,6 @@ def _draw_clust_bezier(ax: plt.Axes, xpos: np.ndarray, ytop: np.ndarray, ybtm: n
150148
fish_data.xst_yst[clone_idx] = (xst, yst)
151149

152150
# Create Path vertices and codes for Bezier
153-
# We need CUBIC4 Bezier: 1 start point, 3 control points per segment.
154-
# The flank points in R seem to influence the curve shape differently than
155-
# standard Bezier control points. Hmisc::bezier might be doing something custom.
156-
# A simpler approach using matplotlib Path with standard quadratic/cubic Beziers
157-
# might not replicate Hmisc::bezier exactly.
158-
# Let's try using Path with the flanked points directly as curve vertices.
159151

160152
# Top path: Start -> Flanked Top Points
161153
x_top_curve = np.concatenate(([xst], x_flanked))
@@ -191,7 +183,7 @@ def _draw_clust_spline(ax: plt.Axes, xpos: np.ndarray, ytop: np.ndarray, ybtm: n
191183
annot_data: Dict[str, Any], clone_idx: int, fish_data: FishPlotData, use_outline: bool = False):
192184
"""Draws a single clone using splined curves."""
193185
if len(xpos) == 0:
194-
print("Skipping drawing for clone with no timepoints.") # Mimic R warning
186+
print("Skipping drawing for clone with no timepoints.")
195187
return
196188
if len(xpos) < 2:
197189
# draw simple polygon if only 1 point
@@ -258,7 +250,7 @@ def _draw_clust_spline(ax: plt.Axes, xpos: np.ndarray, ytop: np.ndarray, ybtm: n
258250
ytop_smooth = spline_top(x_smooth)
259251
ybtm_smooth = spline_btm(x_smooth)
260252

261-
# Clip y values to avoid going outside 0-100 range significantly?
253+
# Clip y values to avoid going outside 0-100 range significantly
262254
# ytop_smooth = np.clip(ytop_smooth, -5, 105) # Generous clipping
263255
# ybtm_smooth = np.clip(ybtm_smooth, -5, 105)
264256

@@ -291,13 +283,12 @@ def _draw_background(ax: plt.Axes, bg_type: str, bg_col: Union[str, List[str]]):
291283
elif bg_type == "gradient":
292284
if not isinstance(bg_col, list) or len(bg_col) != 3:
293285
warnings.warn("Gradient background requires a list of 3 colors. Using default gradient.")
294-
# R's default: c("bisque","darkgoldenrod1","darkorange3")
286+
# "bisque", "darkgoldenrod1", "darkorange3"
295287
bg_col = ['bisque', '#FFB90F', '#CD6600']
296288

297289
# Create a vertical gradient
298290
cmap = mcolors.LinearSegmentedColormap.from_list("fish_gradient", bg_col)
299291
# Draw gradient on a rectangle covering the axes
300-
# Need axes limits to place the image correctly. Use imshow.
301292
# Get axes limits (usually 0-100 for y)
302293
xlim = ax.get_xlim()
303294
ylim = ax.get_ylim()
@@ -533,20 +524,18 @@ def fishplot(fish_data: FishPlotData,
533524
# --- Add Titles ---
534525
title_fontsize = plt.rcParams['font.size'] * cex_title
535526
if title:
536-
# R positions title centrally above the plot. Use fig.suptitle or ax.set_title.
537-
# ax.set_title might overlap vlab. fig.suptitle is often better.
538-
fig.suptitle(title, fontsize=title_fontsize, y=0.95) # Adjust y as needed
527+
# position title centrally above the plot
528+
fig.suptitle(title, fontsize=title_fontsize, y=0.95)
539529

540530
if title_btm:
541-
# R places this at bottom left, slightly outside the main x-range
531+
# place at bottom left, slightly outside the main x-range
542532
x_btm_pos = xlim[0] - pad_left * 0.1 # Position slightly left of padded area
543-
y_btm_pos = 2 # R uses y=2
533+
y_btm_pos = 2
544534
ax.text(x_btm_pos, y_btm_pos, title_btm,
545-
ha='left', va='bottom', # Left align text
546-
fontsize=title_fontsize)
535+
ha='left', va='bottom', fontsize=title_fontsize)
547536

548537
# Adjust layout slightly to prevent titles/labels overlapping axes
549-
# fig.tight_layout(rect=[0, 0.03, 1, 0.95]) # Example adjustment
538+
# fig.tight_layout(rect=[0, 0.03, 1, 0.95])
550539

551540
return fig, ax
552541

@@ -560,7 +549,7 @@ def draw_legend(fish_data: FishPlotData,
560549
cex: float = 1.0,
561550
frameon: bool = False,
562551
title: Optional[str] = None,
563-
title_fontsize: Optional[Union[str, float]] = None, # Added (requires mpl 3.7+)
552+
title_fontsize: Optional[Union[str, float]] = None,
564553
labelspacing: float = 0.5,
565554
columnspacing: float = 1.0,
566555
handletextpad: float = 0.8,
@@ -636,7 +625,7 @@ def draw_legend(fish_data: FishPlotData,
636625
if ax is not None:
637626
fig = ax.get_figure()
638627
else:
639-
fig = plt.gcf() # Get current figure if no context provided
628+
fig = plt.gcf() # get current figure if no context provided
640629

641630
n_clones = fish_data.n_clones
642631
if n_clones == 0:
@@ -660,7 +649,7 @@ def draw_legend(fish_data: FishPlotData,
660649
handles = [patches.Patch(color=color, label=label)
661650
for color, label in zip(fish_data.colors, fish_data.clone_labels)]
662651

663-
fontsize = plt.rcParams['font.size'] * cex * 0.8 # Match R's 0.8 factor
652+
fontsize = plt.rcParams['font.size'] * cex * 0.8
664653

665654
# Prepare legend kwargs, handle title_fontsize conditionally for compatibility
666655
legend_kwargs = {

0 commit comments

Comments
 (0)