2
$\begingroup$

I have a list of points, say

a = Table[{RandomReal[1], {RandomReal[1], RandomReal[1]}}, {i, 100}]

I would like to plot the 2D points in the second column, but colour them using Colorfunction with respect to the first column. I tried

Show[ListPlot[a[[All, 2]], 
  ColorFunction -> (ColorData["SolarColors"][#1] &)]]]

But it didn't work for me? any help is appreciated - thanks!

$\endgroup$
  • 1
    $\begingroup$ Graphics[{ColorData["SolarColors"][#1], AbsolutePointSize[10], Point[#2]} & @@@ a, Axes -> True] $\endgroup$ – OkkesDulgerci Dec 24 '18 at 0:19
  • $\begingroup$ @OkkesDulgerci - to scale the ColorFunction use Graphics[{ColorData[{"SolarColors", MinMax[a[[All, 1]]]}][#1], AbsolutePointSize[10], Point[#2]} & @@@ a, Axes -> True] $\endgroup$ – Bob Hanlon Dec 24 '18 at 0:58
4
$\begingroup$

You can wrap each data point with Style:

ListPlot[Style[#2, ColorData["SolarColors"][#1]] & @@@ a,  BaseStyle -> PointSize[Large]]

enter image description here

Alternatively,

ListPlot[List /@ a[[All, 2]], 
 PlotStyle -> (ColorData["SolarColors"] /@ a[[All, 1]]), 
 BaseStyle -> PointSize[Large]]

same picture

$\endgroup$
  • $\begingroup$ Amazing, thank you; what exactly was wrong with the way I did it? $\endgroup$ – MKF Dec 24 '18 at 0:01
  • $\begingroup$ @MKF, ColorFunction -> ColorData["SolarColors"][#1] & uses the x-coordinate of input data (a[[All,2]]) . $\endgroup$ – kglr Dec 24 '18 at 0:05
  • 1
    $\begingroup$ @MKF - ColorData[{"SolarColors", MinMax[a[[All, 1]]]}]` $\endgroup$ – Bob Hanlon Dec 24 '18 at 1:00
  • 3
    $\begingroup$ @MKF - ListPlot[Style[#2, ColorData[{"SolarColors", MinMax[a[[All, 1]]]}][#1]] & @@@ a, BaseStyle -> PointSize[Large], PlotLegends -> BarLegend[{"SolarColors", MinMax[a[[All, 1]]]}]] $\endgroup$ – Bob Hanlon Dec 24 '18 at 1:11
  • 3
    $\begingroup$ @MKF - Or ListPlot[List /@ a[[All, 2]], PlotStyle -> (ColorData[{"SolarColors", MinMax[a[[All, 1]]]}] /@ a[[All, 1]]), BaseStyle -> PointSize[Large], PlotLegends -> BarLegend[{"SolarColors", MinMax[a[[All, 1]]]}]] $\endgroup$ – Bob Hanlon Dec 24 '18 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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