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' class.
from __future__ import division | |
import matplotlib.pyplot | |
import numpy | |
from segmented_plot import segment_plot | |
base_date = ephem.Date(b'2014/11/25') | |
date_range = numpy.array(range(-182, 2*356)) | |
seg_plotter = segment_plot(lims=[0, 24], thresh=0.95) | |
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'})]: | |
seg_plotter(numpy.array([12*ep.ra/numpy.pi for ep in [planet(base_date + d) for d in date_range]]), | |
date_range, | |
ax, **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' class.
from __future__ import division | |
from matplotlib.pyplot import gca | |
import numpy | |
class segment_plot(object): | |
def __init__(self, lims=[0, 24], thresh=0.95): | |
self.lims = lims | |
self.thresh = thresh | |
self.delta = lims[1] - lims[0] | |
def unlink_wrap(self, dat): | |
jump = numpy.nonzero(numpy.abs(numpy.diff(dat)) > (self.delta * self.thresh))[0] | |
lasti = 0 | |
for ind in jump: | |
yield slice(lasti, ind + 1) | |
lasti = ind + 1 | |
yield slice(lasti, len(dat)) | |
def pad_segments(self, 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] + self.delta])) | |
y_values = numpy.concatenate((y_values, [y[slc.stop + 1]])) | |
if slc.start > 0: | |
x_values = numpy.concatenate(([x[slc.start - 1] - self.delta], x_values)) | |
y_values = numpy.concatenate(([y[slc.start - 1]], y_values)) | |
return x_values, y_values | |
def iter(self, x, y): | |
for slc in self.unlink_wrap(x): | |
yield pad_segments(x, y, slc) | |
def __call__(self, x, y, ax=None, **kwargs): | |
if ax is None: | |
ax = gca() | |
for x_vals, y_vals in self.iter(x, y) | |
ax.plot(x_vals, y_vals, **kwargs) |