You are reading an old version of the documentation (v1.5.0). For the latest version see

We're updating the default styles for Matplotlib 2.0

Learn what to expect in the new updates


Previous topic

pylab_examples example code:

Next topic

pylab_examples example code:

This Page

pylab_examples example code: clippedline.pyΒΆ

(Source code, png, hires.png, pdf)

Clip a line according to the current xlimits, and change the marker
style when zoomed in.

It is not clear this example is still needed or valid; clipping
is now automatic for Line2D objects when x is sorted in
ascending order.


from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import numpy as np

class ClippedLine(Line2D):
    Clip the xlimits to the axes view limits
        this example assumes x is sorted

    def __init__(self, ax, *args, **kwargs):
        Line2D.__init__(self, *args, **kwargs) = ax

    def set_data(self, *args, **kwargs):
        Line2D.set_data(self, *args, **kwargs)
        self.xorig = np.array(self._x)
        self.yorig = np.array(self._y)

    def draw(self, renderer):
        xlim =

        ind0, ind1 = np.searchsorted(self.xorig, xlim)
        self._x = self.xorig[ind0:ind1]
        self._y = self.yorig[ind0:ind1]
        N = len(self._x)
        if N < 1000:
            self._marker = 's'
            self._linestyle = '-'
            self._marker = None
            self._linestyle = '-'

        Line2D.draw(self, renderer)

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False)

t = np.arange(0.0, 100.0, 0.01)
s = np.sin(2*np.pi*t)
line = ClippedLine(ax, t, s, color='g', ls='-', lw=2)
ax.set_xlim(10, 30)
ax.set_ylim(-1.1, 1.1)

Keywords: python, matplotlib, pylab, example, codex (see Search examples)