Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/matplotlib/backends/qt_editor/figureoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def convert_limits(lim, converter):
)
for name, axis in axis_map.items()
]),
('Legend visible', axes.legend_ is not None),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS this does not work, because it does not cover the case of an existing legend with set_visible(False)

('(Re-)Generate automatic legend', False),
]

Expand Down Expand Up @@ -196,6 +197,7 @@ def apply_callback(data):
title = general.pop(0)
axes.set_title(title)
generate_legend = general.pop()
legend_visible = general.pop()

for i, (name, axis) in enumerate(axis_map.items()):
axis_min = general[4*i]
Expand Down Expand Up @@ -256,6 +258,10 @@ def apply_callback(data):
if new_legend:
new_legend.set_draggable(draggable)

# toggle legend visibility
if axes.legend_ is not None:
axes.legend_.set_visible(legend_visible)
Comment on lines +261 to +263

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an issue with this:
Let's say, we don't have a legend, then the checkbox "Legend visible" is unchecked.
If the user checks the box, surpisingly nothing will happen since, we don't have a legend.

Either we would need to generate a legend (not so great beacuse we'd need to assume the defaults, equivalent to a legend() call), or we'd have to deactivate the checkbox if no legend is available (not sure whether that is possible with the generic dialog concept).


# Redraw
figure = axes.get_figure()
figure.canvas.draw()
Expand Down
46 changes: 46 additions & 0 deletions lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,52 @@ def test_figureoptions_with_datetime_axes():
fig.canvas.manager.toolbar.edit_parameters()


@pytest.mark.backend('QtAgg', skip_on_importerror=True)
@pytest.mark.parametrize("legend_visible", [True, False])
def test_legend_checkbox_toggle(legend_visible):
fig, ax = plt.subplots()
ax.plot([1, 2, 3], label='Line 1')
ax.legend()
ax.legend_.set_visible(not legend_visible)
# Set initial visibility opposite to the test case

with mock.patch.object(fig.canvas, 'draw') as mock_draw, \
mock.patch.object(fig.canvas, 'toolbar', create=True):
callback = None
def fake_fedit(datalist, title, parent=None, icon=None, apply=None):
nonlocal callback
callback = apply
return datalist

with mock.patch.object(matplotlib.backends.qt_editor.figureoptions._formlayout,
'fedit', side_effect=fake_fedit):
matplotlib.backends.qt_editor.figureoptions.figure_edit(ax)

xlim = ax.get_xlim()
ylim = ax.get_ylim()
xlabel = ax.get_xlabel()
ylabel = ax.get_ylabel()
xscale = ax.get_xscale()
yscale = ax.get_yscale()

form_data = [
[
"Title",
xlim[0], xlim[1], xlabel, xscale,
ylim[0], ylim[1], ylabel, yscale,
legend_visible,
False
],
[
['Title', '-', 'default', 1.5, '#1f77b4ff', '',
6.0, '#1f77b4ff', '#1f77b4ff']
]
]

callback(form_data)
assert ax.legend_.get_visible() == legend_visible


@pytest.mark.backend('QtAgg', skip_on_importerror=True)
def test_double_resize():
# Check that resizing a figure twice keeps the same window size
Expand Down
Loading