Skip to main content

dynamic - How can I make a clickable ArrayPlot that returns input?


I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use?



Answer




ArrayPlot is much more than just a simple array like Grid: it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange. These features make it quite complicated to reproduce the same layout and order with Grid.


Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled, HighlightCoordinates, HighlightStyle and HighlightElementFunction.


data = {{Missing["HasSomeMoreData"], GrayLevel[
1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]},
RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel[0.5], RGBColor[
1, 1, 0], RGBColor[1, 0.5, 0]}, {GrayLevel[0], GrayLevel[1],
GrayLevel[0], RGBColor[1, 0, 0]}};

{ArrayPlot[data, DataRange -> {{-4, 0}, {-2, 0}}, FrameTicks -> All],
AnnotatedArrayPlot[data, DataRange -> {{-4, 0}, {-2, 0}},

FrameTicks -> All,
HighlightElementFunction -> (Tooltip[
Button[{Rectangle @@ #2}, Print@{#3, #4}], #4] &)]}

enter image description here


The option HighlightElementFunction recieves the following values:



  • #1 = the actual {$x$, $y$} center coordinates of the selected cell within the graphics;

  • #2 = the lower left and upper right corner coordinates of the selected cell;

  • #3 = the {$i$, $j$} indices of the selected cell within the data array;


  • #4 = the selected element from the input data array.


Examples


{{
ArrayPlot[{{1, 0, 0, 0.3}, {1, 1, 0, 0.3}, {1, 0, 1, 0.7}},
ColorRules -> {1 -> Pink, 0 -> Yellow}, ImageSize -> 100],
ArrayPlot[{{RGBColor[1, 0, 0], RGBColor[0, 1, 0], RGBColor[0, 0, 1]},
{RGBColor[0, 0, 1], RGBColor[0, 1, 0]}}, Frame -> True, ImageSize -> 100],
ArrayPlot[{{RGBColor[1, 0, 0], RGBColor[0, 1, 0], RGBColor[0, 0, 1]}},
DataRange -> {{1, 3}, {0, 1}}, FrameTicks -> All, ImageSize -> 100],

ArrayPlot[{{RGBColor[0, 1, 0]}}, Frame -> True, FrameTicks -> All,
ImageSize -> 100, DataRange -> {{1, 2}, {0, 1}}],
ArrayPlot[{{RGBColor[0, 1, 0], RGBColor[0, 0, 1]}}\[Transpose],
Frame -> True, FrameTicks -> All, ImageSize -> 70],
ArrayPlot[RandomReal[1, {10, 20}], ColorFunction -> "Rainbow",
ImageSize -> 100]
},{
AnnotatedArrayPlot[{{1, 0, 0, 0.3}, {1, 1, 0, 0.3}, {1, 0, 1, 0.7}},
ColorRules -> {1 -> Pink, 0 -> Yellow}, ImageSize -> 100],
AnnotatedArrayPlot[{{RGBColor[1, 0, 0], RGBColor[0, 1, 0], RGBColor[0, 0, 1]},

{RGBColor[0, 0, 1], RGBColor[0, 1, 0]}}, Frame -> True, ImageSize -> 100],
AnnotatedArrayPlot[{{RGBColor[1, 0, 0], RGBColor[0, 1, 0],
RGBColor[0, 0, 1]}}, DataRange -> {{1, 3}, {0, 1}}, FrameTicks -> All, ImageSize -> 100],
AnnotatedArrayPlot[{{RGBColor[0, 1, 0]}}, Frame -> True, FrameTicks -> All,
ImageSize -> 100, DataRange -> {{1, 2}, {0, 1}}],
AnnotatedArrayPlot[{{RGBColor[0, 1, 0], RGBColor[0, 0, 1]}}\[Transpose],
Frame -> True, FrameTicks -> All, ImageSize -> 70],
AnnotatedArrayPlot[RandomReal[1, {10, 20}],
ColorFunction -> "Rainbow", ImageSize -> 100]
}} // Grid


Mathematica graphics


Code


resolveSymbolicPosition[pos_, def_List] := Switch[pos,
Center | Left | Right | {Center} | {Left} | {Right}, {pos,
Last@def},
Top | Bottom | {Top} | {Bottom}, {First@def, pos},
{Center | Left | Right, Center | Top | Bottom, ___}, Take[pos, 2],
None | {None} | {None, None}, {None, None},
_Symbol, def,

_?NumericQ, {pos, Last@def},
{__?NumericQ}, PadRight[pos, 2, def],
_, def];

Options[AnnotatedArrayPlot] = Join[Options@ArrayPlot, {
Method -> "Queued",(* NOTE:
By default "Queued" is used so that long calculations are \
finished before preemption. *)
Enabled -> True,(*PlotRangeClipping\[Rule]True,*)
HighlightCoordinates -> None,

HighlightStyle ->
Directive[EdgeForm@{GrayLevel[0, .5], AbsoluteThickness@1},
FaceForm@GrayLevel[1, .3]],
HighlightElementFunction -> (Tooltip[Rectangle @@ #2, #4] &)}];
AnnotatedArrayPlot[data : {__List}, opts : OptionsPattern[]] :=
Module[{dd, emptyQ = data === {{}}, xr, yr, init, fun, dr, rev, ar,
ops, m, n},
{init, fun, dr, rev, ar} =
OptionValue@{HighlightCoordinates, HighlightElementFunction,
DataRange, DataReversed, AspectRatio};

ops = DeleteCases[
FilterRules[Flatten@{opts}, Options@ArrayPlot], _[
Epilog | DataRange | DataReversed, _]];
{m, n} = {Max[Length /@ data], Length@data};
dd = If[ArrayQ@data, data, PadRight[data, {n, m}, None]];
{xr, yr} =
Switch[rev, Automatic, {False, False}, _List,
PadRight[rev, 2, False], _, PadRight[{rev}, 2, False]];
(*Print@{emptyQ,{m,n},{xr,yr},dd};*)


DynamicModule[{rx, ry, px, py, dx, dy, d, x, y, bx, by, i, j, elem,
update, f = fun, rect},
{rx, ry} = Switch[dr, All | Automatic, {{1, m}, {1, n}}, _, dr];
{dx, dy} =
MapThread[
If[#2 == 1, 1, Abs[Subtract @@ #1]/(#2 - 1)] &, {{rx, ry}, {m,
n}}];
{px, py} = {rx + {-dx, dx}/2, ry + {-dy, dy}/2} +
If[dr === All, 0, Switch[{m, n},
{1, 1}, {{dx, 0}, {dy, 0}},

{1, _}, {{dx, 0}, {dy, dy}/2},
{_, 1}, {{dx, dx}/2, {dy, 0}},
_, {{dx, dx}, {dy, dy}}/2]];
rect = Reverse@{py, px}\[Transpose] - {{0, 0}, {dx, dy}};
d = Switch[{xr, yr}, {True, True}, Reverse, {False, False},
Map@Reverse, {False, True},
Reverse /@ Reverse@# &, {True, False}, Identity]@Transpose@dd;
init =
resolveSymbolicPosition[
init, {Left, Bottom}] /. {Left -> First@ry, Right -> Last@ry,

Bottom -> First@rx, Top -> Last@rx};
update[{None, None}] := ({x, y} = {i, j} = {None, None};
elem = None; {bx, by} = {{}, {}});
update[pos_] := (
{x, y} = pos;
{bx, by} = {{x, y} - {dx, dy}/2, {x, y} + {dx, dy}/2};
{i, j} =
Ceiling@MapThread[
Rescale, {{x, y}, {px, py}, {{1, m}, {1, n}}}, 1];
elem = If[emptyQ, Null, d[[i, j]]];);

update@If[emptyQ, {None, None}, init];
LocatorPane[
Dynamic[{x, y}, update@# &],
EventHandler[
ArrayPlot[dd,
DataRange -> dr, DataReversed -> {xr, yr}, ops,
Epilog -> {
OptionValue@Epilog,
If[emptyQ, {},
Flatten@{OptionValue@HighlightStyle,

Dynamic[
If[x === None, {}, f[{x, y}, {bx, by}, {i, j}, elem]],
TrackedSymbols :> {x, y}]}]
}],
"MouseExited" :> ({x, y} = {None, None}; update@{x, y}),
Method -> OptionValue@Method],
Append[rect, {dx, dy}],
AutoAction -> True, Appearance -> None,
LocatorAutoCreate -> False, Enabled -> OptionValue@Enabled]
]];

Comments

Popular posts from this blog

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1....