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.
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) | |