Drawing nices curves with periodic boundaries and without segments

Viewed 241

Using the library pyephem, I would like to find a way to do a nice plot (longitude, latitude) curves which represents the ground track of a satellite. I have computed the (longitude, latitude) but when longitude is over +180°, the next value computed is for example -178°, such that a segment is drawn, that makes bad figure.

Below the code that I have for the moment and the figure obtained.

currentDate = date.datetime(2018,12,1,0,0,0);

for i in range(nPoints):
  iss.compute(currentDate)
  # compute latitude
  posLat[i] = iss.sublat*(180/math.pi)
  # compute longitude
  posLong[i] = iss.sublong*(180/math.pi)
  currentDate = currentDate + date.timedelta(seconds = (time3orbits/nPoints*3600))                
  print currentDate, posLong[i], posLat[i]

plt.plot(posLong,posLat)
plt.show()

Here the figure that I get (for 3 orbits around earth):

enter image description here

How to prevent the segments drawn?

iss.sublat and iss.sublong are repectively the current latitude and longitude of iss position (from a currentDate fixed).

1 Answers

You can split the data array on sign changes from positive to negative and then plot the single sub-arrays:

orbits = np.argwhere(
    (data[:-1, 0] * data[1:, 0] < 0)
    & (data[1:, 0] < 0)
).ravel() + 1

for orbit in np.split(data, orbits, axis=0):
    plt.plot(orbit[:, 0], orbit[:, 1])

enter image description here

Related