Skip to content

Conversation

@krystophny
Copy link
Collaborator

Summary

Fixes legend visibility issue where the legend was not displayed in format_string_demo despite show_legend being set to true.

Root Cause

The figure re-initialization logic in figure_plot_mod.f90 was unconditionally resetting show_legend = .false. during every call to initialize(), overriding user settings.

Solution

Removed the automatic reset of show_legend during figure re-initialization, allowing the user's legend visibility preference to persist across plot operations.

Changes

  • Modified src/figure_plot_mod.f90: Removed this%show_legend = .false. from the initialize subroutine
  • This ensures legend visibility is controlled solely by user calls to legend() method

Testing

Verified that:

  • Legend now appears correctly in format_string_demo when legend() is called
  • Other examples continue to work as expected
  • Legend visibility can be controlled programmatically as intended

Fixes #331

The legend visibility was being reset to false during figure
re-initialization in the savefig process, causing legends to
disappear from all outputs (PNG, PDF, TXT). This happened because:

1. legend() correctly set show_legend = true
2. savefig() deallocated/reallocated the backend
3. ensure_global_figure_initialized() called fig.initialize()
4. initialize() reset show_legend = false (the bug)
5. render_figure() saw show_legend = false and skipped legend

The fix preserves existing legend settings during re-initialization
by removing the automatic reset. Initial legend state is already
correctly initialized to false when the figure is first created.

This fixes legend visibility in format_string_demo and all other
examples that use legends across all output formats.

Fixes #331

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Aug 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@krystophny krystophny merged commit b499427 into main Aug 25, 2025
5 checks passed
@krystophny krystophny deleted the fix-legend-visibility-331 branch August 25, 2025 18:27
krystophny added a commit that referenced this pull request Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No legend visible in https://lazy-fortran.github.io/fortplot/page/examples/format_string_demo.html

2 participants