diff --git a/BACKLOG.md b/BACKLOG.md index bda7818c..b3792187 100644 --- a/BACKLOG.md +++ b/BACKLOG.md @@ -9,7 +9,7 @@ - [x] #334: Fix - No output visible on pcolormesh_demo.html (COMPLETED - PR #351) - [x] #333: Fix - Circles seem not centered with line plot in marker_demo.html (COMPLETED) - [x] #332: Fix - Dashed and dash-dotted look funny on line_styles.html (COMPLETED) -- [ ] #330: Fix - Old plot not cleared in second figure (figure() call) in contour_demo.html +- [x] #330: Fix - Old plot not cleared in second figure (figure() call) in contour_demo.html (COMPLETED) - [ ] #329: Fix - No output visible on colored_contours.html - [ ] #328: Fix - One legend entry too much in basic_plots.html second plot - [ ] #327: Fix - MP4 link not showing on animation.html @@ -24,6 +24,7 @@ - [ ] #350: Refactor - improve documentation comments in raster drawing module ## DOING (Current Work) +- [ ] #330: Fix - Old plot not cleared in second figure (figure() call) in contour_demo.html ## BLOCKED (Infrastructure Issues) diff --git a/src/fortplot_figure_core.f90 b/src/fortplot_figure_core.f90 index dcbc0bde..d1c3b5c2 100644 --- a/src/fortplot_figure_core.f90 +++ b/src/fortplot_figure_core.f90 @@ -146,8 +146,8 @@ subroutine initialize(self, width, height, backend) if (.not. allocated(self%plots)) then allocate(self%plots(self%max_plots)) - self%plot_count = 0 ! Only reset plot count when allocating plots array end if + self%plot_count = 0 ! Reset plot count on every figure() call self%rendered = .false. ! Preserve existing legend settings during re-initialization diff --git a/test/test_figure_clear_330.f90 b/test/test_figure_clear_330.f90 new file mode 100644 index 00000000..ec3afa50 --- /dev/null +++ b/test/test_figure_clear_330.f90 @@ -0,0 +1,38 @@ +program test_figure_clear + !> Test case for Issue #330: Ensure figure() properly clears previous plots + !> This test validates that plot_count is reset when figure() is called multiple times + + use fortplot + use iso_fortran_env, only: real64 + implicit none + + real(real64) :: x(5) = [1.0_real64, 2.0_real64, 3.0_real64, 4.0_real64, 5.0_real64] + real(real64) :: y1(5) = [1.0_real64, 4.0_real64, 2.0_real64, 3.0_real64, 5.0_real64] + real(real64) :: y2(5) = [2.0_real64, 3.0_real64, 1.0_real64, 5.0_real64, 4.0_real64] + + print *, 'Testing figure clearing functionality...' + + ! First figure - should have 1 plot + call figure() + call plot(x, y1) + print *, 'First figure created with 1 plot' + + ! Second figure - should clear previous plot and have only 1 plot + call figure() + call plot(x, y2) + print *, 'Second figure created - previous plot should be cleared' + + ! Test with multiple plots in sequence + call figure() + call plot(x, y1, label='Line 1') + call plot(x, y2, label='Line 2') + print *, 'Third figure with 2 plots' + + ! Clear and add single plot again + call figure() + call plot(x, y1) + print *, 'Fourth figure - should have only 1 plot, not accumulating previous plots' + + print *, 'Figure clearing test completed successfully' + +end program test_figure_clear \ No newline at end of file