2
$\begingroup$

I have data in the form of x = {1,3,5,6,2,4,6,4.5,2.5,2.1,5.7}; y = {3,6,2,1,8,4,3,9,11,16,9}; z = {0.001,0.004,0.012,0.005,0.002,0.003,0.018,0.09,0.007,0.005,0.009};

Data = {1, 3, 0.001}, {3, 6, 0.004}, {5, 2, 0.012}, {6, 1, 0.005},
  {2, 8, 0.002}, {4, 4, 0.003}, {6, 3, 0.018}, {4.5, 9, 0.09},
  {2.5, 11, 0.007}, {2.1, 16, 0.005}, {5.7, 9, 0.009};

I want to plot the 'x' and 'y' axis in ListPlot(Plotstyle->Dot) and want to add the z-axis as a bar legend against each corresponding value of X and Y.

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Commented Feb 2, 2022 at 14:08

1 Answer 1

3
$\begingroup$
Data = Sort[{{1, 3, 0.001}, {3, 6, 0.004}, {5, 2, 0.012}, {6, 1, 
     0.005}, {2, 8, 0.002}, {4, 4, 0.003}, {6, 3, 0.018}, {4.5, 9, 
     0.009}, {2.5, 11, 0.007}, {2.1, 16, 0.005}, {5.7, 9, 0.009}}];


colorBar[arg_] := Blend[{Red, Green, Blue}, Rescale[arg, {0, 0.012}]];
ListPlot[Most /@ Data, PlotRange -> All, 
 ColorFunction :> 
  Function[{x, y}, 
   colorBar[
    Data[[Position[Data[[All, 1]], 
        Nearest[Data[[All, 1]], x][[1]]][[1, 1]], 3]]]], 
 ColorFunctionScaling -> False, 
 PlotLegends -> 
  BarLegend[{{Red, Green, Blue}, {0, 0.012}}, 
   LegendLayout -> "Column", Charting`TickSide -> Right, 
   LabelStyle -> {FontSize -> 13, Black}, LegendMarkerSize -> 200, 
   ColorFunctionScaling -> True, LegendLabel -> "Z"], Frame -> True]   

A slightly modified (simplified) version

Data = Sort[{{1, 3, 0.001}, {3, 6, 0.004}, {5, 2, 0.012}, {6, 1, 
     0.005}, {2, 8, 0.002}, {4, 4, 0.003}, {6, 3, 0.018}, {4.5, 9, 
     0.009}, {2.5, 11, 0.007}, {2.1, 16, 0.005}, {5.7, 9, 0.009}}];
Dataf = Interpolation[Data];
colorBar[arg_] := 
  Blend[{Red, Green, Blue}, Rescale[arg, {0, 0.012}]];
ListPlot[Most /@ Data, PlotRange -> All, 
 ColorFunction :> Function[{x, y}, colorBar[Dataf[x, y]]], 
 ColorFunctionScaling -> False, 
 PlotLegends -> 
  BarLegend[{{Red, Green, Blue}, {0, 0.012}}, 
   ColorFunctionScaling -> True], Frame -> True]

Interpolation is used to find the value of Z for the selected values of {x,y}. Other than that, the code can remain the same.

enter image description here

$\endgroup$
4
  • $\begingroup$ Thank you @MMA 13. Is there any other simple way of achieving the same? $\endgroup$ Commented Jul 11 at 17:22
  • $\begingroup$ The above code could not produce the result presented here in mathematica 11.2 and the current version 13. $\endgroup$ Commented Jul 12 at 0:37
  • $\begingroup$ @Rajendraprasad, try ColorFunction -> Function[{x, y}, colorBar[Data[[First@Nearest[Data[[All, ;; 2]] -> "Index", {x, y}], 3]]]] $\endgroup$
    – MMA13
    Commented Jul 13 at 9:09
  • $\begingroup$ thank you for your response. I could find the issue and resolve it. $\endgroup$ Commented Sep 6 at 15:41

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.