Revision: 8207
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8207&view=rev
Author:   ryanmay
Date:     2010-03-21 17:43:11 +0000 (Sun, 21 Mar 2010)

Log Message:
-----------
Add multicolored line example based on an example from the scipy.org cookbook 
cleaned up to use colormaps.

Added Paths:
-----------
    trunk/matplotlib/examples/pylab_examples/multicolored_line.py

Added: trunk/matplotlib/examples/pylab_examples/multicolored_line.py
===================================================================
--- trunk/matplotlib/examples/pylab_examples/multicolored_line.py               
                (rev 0)
+++ trunk/matplotlib/examples/pylab_examples/multicolored_line.py       
2010-03-21 17:43:11 UTC (rev 8207)
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+'''
+Color parts of a line based on its properties, e.g., slope.
+'''
+import numpy as np
+import matplotlib.pyplot as plt
+from matplotlib.collections import LineCollection
+from matplotlib.colors import ListedColormap, BoundaryNorm
+
+x = np.linspace(0, 3 * np.pi, 500)
+y = np.sin(x)
+z = np.cos(0.5 * (x[:-1] + x[1:]))  # first derivative
+
+# Create a colormap for red, green and blue and a norm to color
+# f' < -0.5 red, f' > 0.5 blue, and the rest green
+cmap = ListedColormap(['r', 'g', 'b'])
+norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
+
+# Create a set of line segments so that we can color them individually
+# This creates the points as a N x 1 x 2 array so that we can stack points
+# together easily to get the segments. The segments array for line collection
+# needs to be numlines x points per line x 2 (x and y)
+points = np.array([x, y]).T.reshape(-1, 1, 2)
+segments = np.concatenate([points[:-1], points[1:]], axis=1)
+
+# Create the line collection object, setting the colormapping parameters.
+# Have to set the actual values used for colormapping separately.
+lc = LineCollection(segments, cmap=cmap, norm=norm)
+lc.set_array(z)
+lc.set_linewidth(3)
+plt.gca().add_collection(lc)
+
+plt.xlim(x.min(), x.max())
+plt.ylim(-1.1, 1.1)
+plt.show()
+


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to