2
$\begingroup$

I have a 2D array with (x, y, val) coordinates. Example,

data = {{1, 1, 0.5}, {1, 0, 0.5), ...}

One can think of the x, y indices as array indices. They are discrete.

I want to create a plot where each (x, y) pair is the starting point of a rectangle (pixel), and val is the (some) color function associated with it. How can I do this with Mathematica?

$\endgroup$
4
$\begingroup$
SeedRandom[1]

xy = RandomSample[Tuples[Range[0, 10], 2], 10];

z = RandomReal[1, 10];

xyz = Join[xy, List /@ z, 2]
{{9, 5, 0.941699}, {7, 3, 0.294264}, {1, 3, 0.188274}, {0, 0, 0.761529},
 {6, 1, 0.169824}, {0, 3, 0.455359}, {5, 10, 0.75425}, {9, 1, 0.268291},
 {2, 1, 0.147377}, {8, 9, 0.480659}}

Graphics

graphics = Graphics[{EdgeForm[Gray], Hue @ #3, Rectangle[{#, #2}]} & @@@ xyz, 
 Frame -> True];

legend = SwatchLegend[Hue /@ xyz[[All, -1]], Defer /@ xyz];

Legended[graphics, legend]

enter image description here

BubbleChart

BubbleChart[xyz, ChartStyle -> (Hue /@ xyz[[All, -1]]), 
 ChartElementFunction -> (Rectangle[Mean /@ #] &), 
 PlotRange -> {{0, 11}, {0, 11}}, 
 ChartLegends -> {Defer /@ xyz}]

enter image description here

DensityHistogram

DensityHistogram[WeightedData[xyz[[All, ;; 2]], xyz[[All, -1]]], {1}, 
 ColorFunction -> Hue, BaseStyle -> EdgeForm[Gray], 
 ChartElementFunction -> (Rectangle[Mean /@ #] &), 
 ColorFunctionScaling -> False, 
 ChartLegends -> SwatchLegend[Hue /@ xyz[[All, -1]], Defer /@ xyz]]

enter image description here

ListPlot

Legended[ReplaceAll[Point -> (Rectangle@*First)]@
  ListPlot[List /@ xyz[[All, ;; 2]], 
   PlotStyle -> Hue /@ xyz[[All, -1]], BaseStyle -> EdgeForm[Gray], 
   AspectRatio -> 1, Frame -> True, PlotRange -> {{0, 10}, {0, 11}}, 
   PlotRangePadding -> .2], legend]

enter image description here

ArrayPlot

ArrayPlot[SparseArray[1 + {#2, #} -> Hue @ #3 & @@@ xyz], 
 DataReversed -> True, 
 FrameTicks -> Table[{# + 1/2, #} & /@ Range[0, 10], 4], 
 PlotLegends -> legend]

enter image description here

Update

To have "the plot legend to look something like the likes of DensityPlot/ArrayPlot", replace legend above with barlegend1 or barlegend2, where

barlegend1 = BarLegend[{Hue, MinMax@xyz[[All, -1]]}];

barlegend2 = BarLegend[{Hue, MinMax@xyz[[All, -1]]}, xyz[[All, -1]]];

Row[{barlegend1, barlegend2}, Spacer[20]]

enter image description here

Replace Hue with ColorData["M10DefaultDensityGradient"] to use the default color scheme for DensityPlot:

barlegend1 = BarLegend[{"M10DefaultDensityGradient", MinMax@xyz[[All, -1]]}];
barlegend2 = BarLegend[{"M10DefaultDensityGradient", MinMax@xyz[[All, -1]]}, 
   xyz[[All, -1]]];

Row[{barlegend1, barlegend2}, Spacer[20]]

enter image description here

Legended[Graphics[{EdgeForm[Gray], Opacity[1], 
   ColorData["M10DefaultDensityGradient"] @ #3, Rectangle[{#, #2}]} & @@@ xyz, 
   Frame -> True], 
  barlegend2]

enter image description here

BubbleChart[xyz, 
 ChartStyle -> (ColorData["M10DefaultDensityGradient"] /@ xyz[[All, -1]]), 
 ChartElementFunction -> (Rectangle[Mean /@ #] &), 
 PlotRange -> {{0, 11}, {0, 11}}, ChartLegends -> barlegend2]

enter image description here

For DensityHistogram, just remove the option ColorFunction -> Hue:

DensityHistogram[WeightedData[xyz[[All, ;; 2]], xyz[[All, -1]]], {1}, 
 ChartElementFunction -> (Rectangle[Mean /@ #] &), 
 ChartLegends -> BarLegend[Automatic, xyz[[All, -1]]]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Looks good, but I need the axes and the plot legends as well! $\endgroup$ – sbp Feb 26 at 5:23
  • $\begingroup$ @sbp, please see the updated version. $\endgroup$ – kglr Feb 26 at 5:31
  • $\begingroup$ Thanks. Cant we have the plot legend to look something like the likes of DensityPlot/ArrayPlot...? $\endgroup$ – sbp Feb 26 at 5:47

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.