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$
2
  • 1
    $\begingroup$ Graphics[{ColorData["SolarColors"][#1], AbsolutePointSize[10], Point[#2]} & @@@ a, Axes -> True] $\endgroup$ Commented Dec 24, 2018 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
    Commented Dec 24, 2018 at 0:58

1 Answer 1

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$
9
  • $\begingroup$ Amazing, thank you; what exactly was wrong with the way I did it? $\endgroup$
    – MKF
    Commented Dec 24, 2018 at 0:01
  • $\begingroup$ @MKF, ColorFunction -> ColorData["SolarColors"][#1] & uses the x-coordinate of input data (a[[All,2]]) . $\endgroup$
    – kglr
    Commented Dec 24, 2018 at 0:05
  • 1
    $\begingroup$ @MKF - ColorData[{"SolarColors", MinMax[a[[All, 1]]]}]` $\endgroup$
    – Bob Hanlon
    Commented Dec 24, 2018 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
    Commented Dec 24, 2018 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
    Commented Dec 24, 2018 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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