Re: [Matplotlib-users] (no subject)

 Re: [Matplotlib-users] (no subject) From: Tony Yu - 2012-01-29 16:20:23 Attachments: Message as HTML ```On Sun, Jan 29, 2012 at 2:21 AM, Paul Hobson wrote: > There is undoubtedly a more efficient way to do this, but give this a shot: > > import numpy as np > import matplotlib.pyplot as plt > > x = np.arange(0, 10.5, 0.5) > y = -3.0*x + 0.5*x**2 > > color_list = ['FireBrick', 'Orange', 'DarkGreen', 'DarkBlue', 'Indigo'] > limits = np.arange(0, 11, 2) > fig, ax1 = plt.subplots() > for n, color in enumerate(color_list): > lower = np.where(x >= limits[n])[0] > upper = np.where(x <= limits[n+1])[0] > index = np.intersect1d(lower, upper) > ax1.plot(x[index], y[index], linestyle='-', color=color, linewidth=2) > > plt.show() > > HTH, > -paul > Alternatively, you could replace the loop above with:: indexes = np.searchsorted(x, limits) # add 1 to end index so that segments overlap for i0, i1, color in zip(indexes[:-1], indexes[1:]+1, color_list): ax1.plot(x[i0:i1], y[i0:i1], linestyle='-', color=color, linewidth=2) This is not much different than Paul's example---just whatever you find more readable. -Tony > > On Fri, Jan 27, 2012 at 8:12 AM, nahren manuel > wrote: > > Dear Users, > > I want to plot a XY, the X-value is constant, but let assume Y varees > from > > 1-10, so I want o have different colors accordingly for the range > > 0-2,2-4,4-6,6-8,8-10. > > > > thanks a lot > > najren > > > > > ------------------------------------------------------------------------------ > > Try before you buy = See our experts in action! > > The most comprehensive online learning library for Microsoft developers > > is just \$99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > > Metro Style Apps, more. Free future releases when you subscribe now! > > http://p.sf.net/sfu/learndevnow-dev2 > > _______________________________________________ > > Matplotlib-users mailing list > > Matplotlib-users@... > > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > > ------------------------------------------------------------------------------ > Try before you buy = See our experts in action! > The most comprehensive online learning library for Microsoft developers > is just \$99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-dev2 > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > ```