(no subject)

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

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

···

On Fri, Jan 27, 2012 at 8:12 AM, nahren manuel <meetnahren@...9...> 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@lists.sourceforge.net
matplotlib-users List Signup and Options

Paul Hobson, on 2012-01-28 23:21, 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)

Another way (if you're ok with only coloring the markers) would
be to use ax1.scatter(x,y,c=colorlist) where the length of all
three arguments is the same. Scatter can do that with the size of
the markers by passing the s= argument.

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

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.0x + 0.5x**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 Sun, Jan 29, 2012 at 2:21 AM, Paul Hobson <pmhobson@…120…287…> wrote:

On Fri, Jan 27, 2012 at 8:12 AM, nahren manuel <meetnahren@…9…> 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@lists.sourceforge.net

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@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users