Commit deb2ec3
authored
fix: reset plot_count unconditionally during figure initialization (#354)
## Summary
- Fixes Issue #330: "Fix - Need to check figure clearing -
contour_demo.html has overlapping contours"
- Root cause: `plot_count` was only reset when first allocating the
plots array, causing subsequent `figure()` calls to accumulate plots
instead of clearing them
- Solution: Move `plot_count = 0` outside the allocation check to reset
unconditionally during every `initialize()` call
## Changes
- **Fixed `initialize()` method** in `src/fortplot_figure_core.f90` to
reset `plot_count` unconditionally
- **Added test case** `test/test_figure_clear_330.f90` to validate
proper figure clearing behavior
- **Preserves allocation logic** - plots array is still only allocated
once for efficiency
## Test Plan
- [x] Created focused test case for figure clearing functionality
- [x] Verified test passes with fix applied
- [x] Ran contour_demo to confirm no more overlapping contours
- [x] Ran related figure tests to ensure no regressions
- [x] All tests pass successfully
## Technical Details
**Before Fix:**
```fortran
if (.not. allocated(self%plots)) then
allocate(self%plots(self%max_plots))
self%plot_count = 0 ! Only reset when allocating
end if
```
**After Fix:**
```fortran
if (.not. allocated(self%plots)) then
allocate(self%plots(self%max_plots))
end if
self%plot_count = 0 ! Reset on every figure() call
```
**Expected Behavior After Fix:**
1. First `figure()` call → allocates plots array and sets plot_count = 0
2. Add plots → plot_count increases normally
3. Second `figure()` call → **resets plot_count = 0** (plots array
already allocated)
4. Add new plots → start from index 0, replacing previous plots
This ensures proper plot isolation between figure() calls and resolves
the overlapping contours issue.
fixes #3301 parent b0f6cf8 commit deb2ec3
File tree
3 files changed
+41
-2
lines changed- src
- test
3 files changed
+41
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
150 | 149 | | |
| 150 | + | |
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
0 commit comments