Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I'm a relative beginner in Mathematica, but after reading the forums for a long time, I couldn't understand one thing:

  • I want to plot a list h. Let's call it ListPlot[h]
  • I also have a second list, of the same size c
  • I want to write a color function that plots a given point in h with a color corresponding to the value of c at the same point.

Anyone knows how to do it?

share|improve this question

marked as duplicate by R. M. Jan 22 '14 at 18:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Related question:… – Cassini Jan 22 '14 at 17:56

2 Answers 2

Perhaps it's better to use Graphics where you have full control over all aspects of the plot:

h = RandomReal[10, {10, 2}];
c = ColorData[1, "ColorList"][[1 ;; 10]];
Graphics[{PointSize[Medium], #[[1]], Point[#[[2]]]} & /@ Transpose[{c, h}], Axes -> True]


Regarding your comment, you could do something like this:

h = RandomReal[10, 20];
c = RandomReal[10, 20];

(* You have to give each y value a corresponding x value *)
h = MapIndexed[{First@#2, #} &, h];

(* You have to normalize the concentration so it works with the color function. *)
c = Normalize[c];

colorfunc = ColorData["SolarColors"];

Graphics[{PointSize[Medium], colorfunc@#[[1]], Point[#[[2]]]} & /@ Transpose[{c, h}], Axes -> True]

It's the same idea, but we don't assume that the x coordinate of each point is its position in the list. We also don't assume a list of colors, but rather think of that list as a list of concentration levels.

I noticed now that in m_goldberg's answer he sets the AspectRatio to 1/GoldenRatio, this is an excellent idea since it looks good and is the default aspect ratio for plots in MMA. I too recommend that.

share|improve this answer
@Edouard, for a large number of points I would suggest using a gradient color scheme, for example: c = ColorData["Rainbow"] /@ (Range[25]/25); – Simon Woods Jan 22 '14 at 16:58
Thanks a lot ! But actually, what I want to do is not to plot one function as a function of the other. I would like h to be a [1xn] list, and simply plot the n values of "h". But then, to have the values of "h" colorcoded according to the values of "c" at the same x-axis value (without actually plotting "c"). Basically, I want to look at the profile of a a physical interface which has some concentration of molecule at every point. And I would like the interface h(x) to be red (for instance) in regions where the concentration c(x) is high ? Thanks again ! – Edouard Jan 22 '14 at 17:03
@Edouard I added something I think should meet your needs. – Pickett Jan 22 '14 at 17:24
Thank you so much, this is awesome ! – Edouard Jan 22 '14 at 17:33

I think Pickett is right to use Graphics, but here is how I would implement it, step-by-step.

Make some data

h = RandomInteger[42, 12];
c = Table[Hue[i/Length[h]], {i, 0, Length[h] - 1}];

Make the points.

pts = Point /@ Transpose[{Range[Length[h]], h}]

Color the points.

coloredPts = Transpose[{c, pts}];

Make a plot with 'Graphics`.

Graphics[{PointSize[Medium], coloredPts},
  Axes -> True, AspectRatio -> 1/GoldenRatio]


share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.