How to remove vertical lines when plotting rotating phase

Forwarding an email that I sent directly to Nikolaus. (I think every other mailing list that I used defaults to something like “Reply to list” or “Reply to all”.)



---------- Forwarded message ----------
From: Warren Weckesser <warren.weckesser@…1036…>
Date: Wed, Apr 4, 2012 at 11:18 AM

Subject: Re: [Matplotlib-users] How to remove vertical lines when plotting rotating phase
To: Nikolaus Rath <Nikolaus@…3072…>

On Wed, Apr 4, 2012 at 10:48 AM, Warren Weckesser <warren.weckesser@…1036…> wrote:

On Wed, Apr 4, 2012 at 10:30 AM, Nikolaus Rath <Nikolaus@…3072…> wrote:


I’m plotting a rotating phase:

ipython --pylab

x1 = linspace(0, 2*pi, 30)

x = concatenate((x1,x1,x1,x1))


The resulting plot has ugly vertical lines whenever x wraps from 2*pi

back to zero.

Does someone have a nice, general way to get to get rid of such lines?

(the actual data is of course less predictable and regular than the

example above).

You can use numpy.unwrap, e.g.



You might not want the “unwrapped” effect. You can figure out where the “big” jumps occur, and plot the data in pieces with something like this example. It doesn’t plot lines that jump by more than pi:

import numpy as np

import matplotlib.pyplot as plt

t = np.linspace(0, 10, 200)

y = np.mod(2 * t * np.sin(0.5 * t), 2 * np.pi)

jumps = np.r_[0, np.where(np.abs(np.diff(y)) > np.pi)[0] + 1, y.size]

for k in range(jumps.size-1):
start, end = jumps[k:k + 2]

plt.plot(t[start:end], y[start:end], 'b')

I don’t know if something like this already exists in matplotlib, or if there is some other technique that would be simpler.