Skip to content

Instantly share code, notes, and snippets.

@lkilcher
Forked from orome/Segmented Plot
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkilcher/d04ed05ad9059e94ca01 to your computer and use it in GitHub Desktop.
Save lkilcher/d04ed05ad9059e94ca01 to your computer and use it in GitHub Desktop.
Segmented Plot
import ephem
from segmented_plot import segment_plot
import matplotlib.pyplot
base_date = ephem.Date(b'2014/11/25')
date_range = numpy.array(range(-182, 2*356))
fig, ax = matplotlib.pyplot.subplots()
for (planet, style) in [(ephem.Moon, {'color': '0.9'}),
(ephem.Sun, {'color': 'y', 'lw': '3'}),
(ephem.Mars, {'color': 'r'}),
(ephem.Jupiter, {'color': 'g'})]:
segment_plot(numpy.array([12*ep.ra/numpy.pi for ep in [planet(base_date + d) for d in date_range]]),
date_range,
ax, lims=[0, 24], thresh=0.95,
**style)
ax.set(xlim=[0, 24])
ax.set(ylim=[min(date_range), max(date_range)])

This file sets the title of this gist.

This gist gives an example of how to implement SO question 27138751 by defining a 'segment_plot' function.

from __future__ import division
from matplotlib.pyplot import gca
import numpy
def unlink_wrap(dat, lims=lims, thresh=thresh):
jump = numpy.nonzero(numpy.abs(numpy.diff(dat)) > ((lims[1] - lims[0]) * thresh))[0]
lasti = 0
for ind in jump:
yield slice(lasti, ind + 1)
lasti = ind + 1
yield slice(lasti, len(dat))
def pad_segments(x, y, slc):
x_values = x[slc]
y_values = y[slc]
if slc.stop < len(x):
x_values = numpy.concatenate((x_values, [x[slc.stop + 1] + 24]))
y_values = numpy.concatenate((y_values, [y[slc.stop + 1]]))
if slc.start > 0:
x_values = numpy.concatenate(([x[slc.start - 1] - 24], x_values))
y_values = numpy.concatenate(([y[slc.start - 1]], y_values))
return x_values, y_values
def segment_plot(x, y, ax=None, lims=[0, 24], thresh=0.95, **kwargs):
if ax is None:
ax = gca()
for slc in unlink_wrap(x, lims=lims, thresh=thresh):
x_vals, y_vals = pad_segments(x, y, slc)
ax.plot(x_vals, y_vals, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment