Skip to content

Commit

Permalink
fixed bug with legend of plot_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide-sd committed Apr 11, 2024
1 parent 3c54648 commit af7b20d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
7 changes: 5 additions & 2 deletions spb/backends/base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,11 @@ def __init__(self, *args, **kwargs):
self.legend = kwargs.get("legend", None)
if self.legend is None:
series_to_show = [
s for s in self._series if (s.show_in_legend and (not s.use_cm)
and (not s.is_grid))
s for s in self._series if (
s.show_in_legend and
(not s.use_cm) and
(not s.is_grid)
)
]
if len(series_to_show) > 1:
# don't show the legend if `plot_piecewise` created this
Expand Down
2 changes: 2 additions & 0 deletions spb/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.is_filled = kwargs.get("is_filled", kwargs.get("fill", True))
self.show_clabels = kwargs.get("clabels", True)
self.use_cm = True

# NOTE: contour plots are used by plot_contour, plot_vector and
# plot_complex_vector. By implementing contour_kw we are able to
Expand Down Expand Up @@ -2709,6 +2710,7 @@ def __init__(self, expr, r, label="", **kwargs):
if not kwargs.get("threed", False):
self.is_contour = True
self.is_3Dsurface = False
self.use_cm = True
self.is_filled = kwargs.get("is_filled", kwargs.get("fill", True))
self.show_clabels = kwargs.get("clabels", True)
self._post_init()
Expand Down
34 changes: 33 additions & 1 deletion tests/backends/test_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
plot_complex_list, graphics, vector_field_2d, plot_nyquist, plot_nichols,
plot_step_response
)
from spb.series import RootLocusSeries, SGridLineSeries, ZGridLineSeries
from spb.series import (
RootLocusSeries, SGridLineSeries, ZGridLineSeries, ContourSeries,
Vector2DSeries
)
from spb.series import SurfaceOver2DRangeSeries
from sympy import (
sin, cos, I, pi, Eq, exp, Circle, Polygon, sqrt, Matrix, Line, Segment,
Expand Down Expand Up @@ -2603,3 +2606,32 @@ def test_hvlines():
)
p.backend.update_interactive({a: 3, b: 4})
p.backend.close()


def test_plot_vector_2d_legend_1():
x, y = symbols("x, y")
p = plot_vector(
[-sin(y), cos(x)], (x, -pi, pi), (y, -pi, pi),
backend=MB, scalar=True, n=10, show=False
)
assert isinstance(p[0], ContourSeries)
assert isinstance(p[1], Vector2DSeries)
assert not p[1].use_cm
# this is because there is only one vector series with use_cm=False
assert p.legend is None

@pytest.mark.parametrize(
"streamlines, use_cm, expected", [
(False, False, True),
(False, True, None),
(True, False, True),
(True, True, None)
]
)
def test_plot_vector_2d_legend_2(streamlines, use_cm, expected):
x, y = symbols("x, y")
p = plot_vector(
[-sin(y), cos(x)], [cos(x), -sin(y)], (x, -pi, pi), (y, -pi, pi),
backend=MB, n=10, show=False, streamlines=streamlines, use_cm=use_cm
)
assert p.legend is expected
2 changes: 1 addition & 1 deletion tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ def test_use_cm(use_cm):
sqrt(z), (z, -2 - 3j, 4 + 5j),
threed=False, use_cm=use_cm
)
assert s.use_cm is use_cm
assert s.use_cm is True

s = ComplexSurfaceSeries(
sqrt(z), (z, -2 - 3j, 4 + 5j),
Expand Down

0 comments on commit af7b20d

Please sign in to comment.