Skip to content

Commit

Permalink
Fix plot_maneuver markers and legend order
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge M.G committed Nov 21, 2021
1 parent c82e492 commit f8cdf46
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
44 changes: 25 additions & 19 deletions src/poliastro/plotting/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
from astropy import units as u
from astropy.coordinates import CartesianRepresentation, concatenate_representations
from astropy.coordinates import CartesianRepresentation

from poliastro.ephem import Ephem
from poliastro.frames import Planes
Expand Down Expand Up @@ -160,8 +160,6 @@ def _plot_maneuver(
"set_attractor(Major_Body) or plot(orbit)"
)

colors = self._get_colors(color, trail)

# Apply the maneuver, collect all intermediate states and allocate the
# final coordinates list array
*maneuver_phases, final_phase = initial_orbit.apply_maneuver(
Expand All @@ -170,13 +168,27 @@ def _plot_maneuver(

if len(maneuver_phases) == 0:
# For single-impulse maneuver only draw the impulse marker
return ([self._draw_impulse(color, f"Impulse - {label}", final_phase.r)],)
impulse_label = f"Impulse 1 - {label}"
impulse_lines = ([self._draw_impulse(color, impulse_label, final_phase.r)],)
return [(impulse_label, impulse_lines)]
else:
coordinates_list = []
# Declare for holding (label, lines) for each impulse and trajectory
lines_list = []

# Collect the coordinates for the different maneuver phases
for ith_impulse, orbit_phase in enumerate(maneuver_phases):

# Plot the impulse marker and collect its label and lines
impulse_label = f"Impulse {ith_impulse + 1} - {label}"
impulse_lines = (
[self._draw_impulse(color, impulse_label, orbit_phase.r)],
)
lines_list.append((impulse_label, impulse_lines))

# HACK: if no color is provided, get the one randomly generated
# for previous impulse lines
color = impulse_lines[0][0].get_color() if color is None else color

# Get the propagation time required before next impulse
time_to_next_impulse, _ = maneuver.impulses[ith_impulse + 1]

Expand All @@ -198,25 +210,19 @@ def _plot_maneuver(
phase_coordinates = orbit_phase.sample(
min_anomaly=min_nu, max_anomaly=max_nu
)
coordinates_list.extend(phase_coordinates)

# Plot the impulse marker
self._draw_impulse(
color, f"Impulse {ith_impulse + 1} - {label}", orbit_phase.r
# Plot the phase trajectory and collect its label and lines
trajectory_lines = self._plot_trajectory(
phase_coordinates, label=label, color=color, trail=trail
)
lines_list.append((label, trajectory_lines))

# Finally, draw the impulse at the very beginning of the final phase
self._draw_impulse(
color, f"Impulse {ith_impulse + 2} - {label}", final_phase.r
)

# Concatenate the different phase coordinates into a single coordinates
# instance.
coordinates = concatenate_representations(coordinates_list)
impulse_label = f"Impulse {ith_impulse + 2} - {label}"
impulse_lines = ([self._draw_impulse(color, impulse_label, final_phase.r)],)
lines_list.append((impulse_label, impulse_lines))

return self.__add_trajectory(
coordinates, None, label=str(label), colors=colors, dashed=False
)
return lines_list

def _plot(self, orbit, *, label=None, color=None, trail=False):
colors = self._get_colors(color, trail)
Expand Down
23 changes: 16 additions & 7 deletions src/poliastro/plotting/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,29 @@ def _get_colors(self, color, trail):

return colors

def _draw_marker(self, marker, size, color, name, center=None):
def _draw_marker(self, marker, size, mew, color, name, center=None):
x_center, y_center = self._project(
center[None]
) # Indexing trick to add one extra dimension

(l,) = self._ax.plot(
x_center.to_value(u.km), y_center.to_value(u.km), "o", mew=0, color=color
x_center.to_value(u.km),
y_center.to_value(u.km),
marker,
markersize=size,
mew=mew,
color=color,
label=name,
)

return l

def _draw_point(self, radius, color, name, center=None):
point_marker = self._draw_marker("o", 0, color, name, center)
point_marker = self._draw_marker("o", 5, None, color, name, center)
return point_marker

def _draw_impulse(self, color, name, center=None):
impulse_marker = self._draw_marker("x", 2, color, name, center)
impulse_marker = self._draw_marker("x", 10, 3, color, name, center)
return impulse_marker

def _draw_sphere(self, radius, color, name, center=[0, 0, 0] * u.km):
Expand Down Expand Up @@ -228,14 +234,17 @@ def plot_maneuver(
"set_orbit_frame(orbit) or plot(orbit)"
)

lines = self._plot_maneuver(
# Collect the labels and lines for each one of the phases of the maneuver
lines_list = self._plot_maneuver(
initial_orbit, maneuver, label=label, color=color, trail=trail
)

# Generate the legend labels in the same order as the maneuver impulses
# are executed
if label:
self._set_legend(label, *lines)
[self._set_legend(lines_label, *lines) for lines_label, lines in lines_list]

return lines
return lines_list

def plot(self, orbit, *, label=None, color=None, trail=False):
"""Plots state and osculating orbit in their plane.
Expand Down
Binary file modified tests/tests_plotting/baseline/test_plot_maneuver.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/tests_plotting/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def test_plot_maneuver():
# Plot the maneuver
fig, ax = plt.subplots()
plotter = StaticOrbitPlotter(ax=ax)
plotter.plot(ss_i, color="blue", label="Initial orbit")
plotter.plot_maneuver(ss_i, man)
plotter.plot(ss_i, label="Initial orbit", color="blue")
plotter.plot_maneuver(ss_i, man, label="Hohmann maneuver", color="red")

return fig

0 comments on commit f8cdf46

Please sign in to comment.