# Create a color gradient using a variable?

**URL:** <https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774>\
**Category:** Questions\
**Tags:** physics, graphics-and-visualization\
**Created:** [September 24, 2020, 7:23pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774 "2020-09-24T19:23:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [September 24, 2020, 7:23pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/1 "2020-09-24T19:23:54Z")

</div>

Let say I have a function

```
F[x_,y_]:=x^2+6y^(3/2)

```

Now I want to plot a 2D plot of F[] vs x, and need to use y variable as a color gradient. Here I want to vary y as a color axis and the values of F will be plotted against x it will be like this

 ![images_OP_1_CP_.png](https://global.discourse-cdn.com/wolfram/original/2X/a/a17493bc06f9b67a429e59cfbcc469453f814b0c.png)  
but with different function

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [September 25, 2020, 2:07am UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/2 "2020-09-25T02:07:32Z")

</div>

Hi John,

Not clear what you are asking for. Maybe `ContourPlot` or `DensityPlot`?

```
ContourPlot[f[x, y], {x, -10, 10}, {y, 0, 10},
 Contours -> 10,
 ContourLabels -> All,
 ColorFunction -> "Rainbow",
 PlotRangePadding -> Scaled[.04]]

DensityPlot[f[x, y], {x, -10, 10}, {y, 0, 10},
 ColorFunction -> "Rainbow"]

```

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [September 25, 2020, 4:47am UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/3 "2020-09-25T04:47:17Z")

</div>

I need this type of plot having y as the color variable

 ![images_OP_1_CP_.png](https://global.discourse-cdn.com/wolfram/original/2X/a/a17493bc06f9b67a429e59cfbcc469453f814b0c.png)

---

<div class="post-metadata">

**Author:** ![xzczd](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/xzczd/32/21428_2.png) [@xzczd](https://community.wolfram.com/u/xzczd)\
**Post date:** [September 30, 2020, 6:47am UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/4 "2020-09-30T06:47:13Z")

</div>

Cross posted: [https://mathematica.stackexchange.com/q/231010/1871](https://mathematica.stackexchange.com/q/231010/1871)

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [September 30, 2020, 9:41am UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/5 "2020-09-30T09:41:33Z")

</div>

Like this? I think you will have to adapt the Hue-expression

```
Plot[
 Cos[x] Exp[- .1 x^2], {x, -5, 5},
 PlotStyle -> Thick,
 ColorFunction -> Function[{x, y}, Hue[y]]
 ]

```

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [September 30, 2020, 5:49pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/6 "2020-09-30T17:49:33Z")

</div>

Or this?

```
F[x_, y_] := x^2 + 6 y^(3/2)

Plot3D[F[x, y], {x, -3, 3}, {y, 0, 4}, ColorFunction -> Function[{x, y, z}, Hue[y]] ]

```

---

<div class="post-metadata">

**Author:** ![yasmin](https://avatars.discourse-cdn.com/v4/letter/y/71c47a/32.png) [@yasmin](https://community.wolfram.com/u/yasmin)\
**Post date:** [October 1, 2020, 3:15am UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/7 "2020-10-01T03:15:04Z")

</div>

I still don’t understand the question? What are you asking for? Kind of lost here.

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [October 1, 2020, 4:49pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/8 "2020-10-01T16:49:54Z")

</div>

I am now explaining the problem with more detailed way, Let’s consider

```
F[x_,y_]:=Sqrt[(5*10^-6)*x^2*(5*10^-9)^y]
y=Sqrt[Tan[x]]

```

Now I have to randomly vary x to some range and make it the color bar axis and the values for F[x,y] and y for each x are plotted in y vs F[x,y]

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 4, 2020, 3:05pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/9 "2020-10-04T15:05:03Z")

</div>

???

What do you want to do?

Perhaps you can give a handmade example?

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [October 4, 2020, 3:41pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/10 "2020-10-04T15:41:29Z")

</div>

v = 246  
E1 = 9000

```
PT = ((Subscript[E1, T]*v)/(
   Subscript[v, n]*E1))/((Subscript[v, n]/Subscript[T, n])^-1*(v/
    E1)) - 7*(Log[Subscript[v, n]]/Subscript[T, n]) + 
  Log[Subscript[T, n]/100]
Subscript[v, n]/Subscript[T, n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Subscript[T, n] = {300, 500, 469, 650, 546, 389, 456, 411, 523, 700};
((Subscript[E1, T]*v)/(Subscript[v, n]*E1)) = 
 RandomReal[{1, 3.1}, {10}]

```

Now I have to randomly vary the last ratio to some range and make it the color bar axis and the values for PT and v\_n/T\_n for each ratios can be plotted PT vs v\_n/T\_n plot making the ratio as color axis

 ![IMG_20201003_211951.jpg](https://global.discourse-cdn.com/wolfram/original/2X/e/ef029876225745fe18a4bdef0d40008e3ac999ed.jpeg)

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 4, 2020, 9:17pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/11 "2020-10-04T21:17:27Z")

</div>

OK. But you are merely repeating your words. For me it is not all all clear what you want to do. And: I suggest that you eliminate all these subscript-expressions. They may be a reason for a lot of mistakes

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 5, 2020, 11:09am UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/12 "2020-10-05T11:09:24Z")

</div>

Hi John,

your code gives error messages because of synthax.mistakes. I had a close look at it and concluded that you want to calculate PT and do it like this

```
v = 246
E1 = 9000

Tn = {300, 500, 469, 650, 546, 389, 456, 411, 523, 700}
vn = Range[10] Tn
E1T = E1 vn RandomReal[{1, 3.1}, 10]/ v
PT = E1T/Tn - 7 Log[vn]/Tn + Log[Tn/100]

```

Is this right?

But a Plot doesn’t look like yours

```
ListLinePlot[Transpose[{vn/Tn, PT}]]

```

Any comments?

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [October 5, 2020, 12:02pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/13 "2020-10-05T12:02:03Z")

</div>

v = 246  
E1 = 9000

```
PT = ((ET*v)/(
   vn*E1))/((vn/Tn)^-1*(v/
    E1)) - 7*(Log[vn]/Tn) + 
  Log[Tn/100];
vn/Tn= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Tn= {300, 500, 469, 650, 546, 389, 456, 411, 523, 700};
((ET*v)/(vn*E1)) := 
 RandomReal[{1, 3.1}, {10}];

```

Now I have to plot between `(vn/Tn)` vs `PT` making a variable color axis of `((ET*v)/(vn*E1))` as like the attatched graphics( it should look like a set of data points but having varing colors depending on `((ET*v)/(vn*E1))`) , The plot may not exactly look like it since I have putted some mannual data by hand

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 5, 2020, 12:27pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/14 "2020-10-05T12:27:40Z")

</div>

Hi John,

didn’t you understand? Your code is WRONG, Have you ever run it? I tried to fix the flaws and asked you if you agree to the results of the new version. But you did not answer to that question. So: please check it.

Then ((ET_v)/(vn_E1)) is the array of the random numbers. Check this too.

After that we can talk about colors.

Here is a 1st attempt

```
v = 246
E1 = 9000

Tn = {300, 500, 469, 650, 546, 389, 456, 411, 523, 700}
vn = Range[10] Tn
sumpt = Table[0, {10}]
nn = 1000
Do[
 rr = RandomReal[{1, 3.1}, 10];
 E1T = E1 vn rr/ v;
 PT = E1T/Tn - 7 Log[vn]/Tn + Log[Tn/100];
 sumpt = sumpt + PT,
 {nn}]
sumpt = sumpt/nn
col = Table[1 + (j - 1) 2.1/9, {j, 1, 10}]
ListLinePlot[
 Transpose[{vn/Tn, PT}],
 PlotStyle -> Thick,
 ColorFunction -> Function[{x, y}, Hue[col[[x]]]],
 ColorFunctionScaling -> False
 ]

```

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [October 5, 2020, 2:30pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/15 "2020-10-05T14:30:24Z")

</div>

Thanks for giving your valueable time, I think now it is exactly what I needed, but how to assign the correct color axis, and also guide me how to draw the color line in case of log color data(i.e. a log color axis)

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [October 5, 2020, 2:45pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/16 "2020-10-05T14:45:21Z")

</div>

Also I have failed to understand what is the meaning of this part

```
Do[
 rr = RandomReal[{1, 3.1}, 10];
 E1T = E1 vn rr/ v;
 PT = E1T/Tn - 7 Log[vn]/Tn + Log[Tn/100];
 sumpt = sumpt + PT,
 {nn}]
sumpt = sumpt/nn
col = Table[1 + (j - 1) 2.1/9, {j, 1, 10}]

```

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 5, 2020, 3:47pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/17 "2020-10-05T15:47:18Z")

</div>

What do you mean by " the correct color axis " ? The colors are coded accroding to the random number used in the calculation fpr the point in question , your ((ETv)/(vnE1)) .

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 5, 2020, 3:50pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/18 "2020-10-05T15:50:18Z")

</div>

This is the calculation of averages. The code for PT is executed nn times, the results are added and finally divided by nn. I have no idea whether you wanted such a thing. col is the range of colors.

---

<div class="post-metadata">

**Author:** ![darkknightxen2134](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@darkknightxen2134](https://community.wolfram.com/u/darkknightxen2134)\
**Post date:** [October 5, 2020, 4:16pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/19 "2020-10-05T16:16:16Z")

</div>

There in the posted image there is a color axis corresponding to the ratio, It is important since it makes the graphics more detailed, the color axis shown in above figure shows how the colors corresponding to each points related to the values of `((ET*v)/(vn*E1))`

Secondly, I only want simply the values of PT (for each random number i.e. one point for each set of data, don’t need any avg like thing) plotted against vn/Tn

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [October 5, 2020, 5:51pm UTC](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774/20 "2020-10-05T17:51:29Z")

</div>

Ok. State now is: you have all the hints that you should be able to do what you want.

[Next page](https://community.wolfram.com/t/create-a-color-gradient-using-a-variable/16774.md?page=2)
