Skip to main content

plotting - How to embed graphic-element-specific metadata within a Graphics object?


One of the most crucial requirements for rich, interactive scientific graphics is being able to "annotate" individual graphic elements (e.g. the data points of a scatterplot, the individual curves in a plot of multiple curve fits to data, individual subregions of a density plot) with additional information. I'm using the term "annotate" very broadly here to stand for "associate additional information with". Minimally, one would like to be able to assign unique identifiers to each feature of interest, that can be used as keys in auxiliary data structures.


(For example, one way to make a scatterplot interactive is to equip each data point in the scatterplot with a tooltip that will display additional information about that point when the user hovers the cursor over the point. Another way would be to give the user the ability to "light up" a subset of the data points based on some shared metadata value.)


Can the Mathematica Graphics object accommodate such annotations within it?


(NB: I don't doubt that it would be possible to implement this sort of capability "on top of" Mathematica. At the moment I am interested only in whether such functionality is already built into Mathematica.)


EDIT: added one more example of possible modes of interactivity.



Answer



I suggest the dictionary data structure, to create semantic and maintainable code.



The most common approach that I've seen on here for handling related data is to stick it all in a list, so people end up with code like this:


{city[[54, 1]], Tooltip[Disk[Reverse@city[[54, 2]], 0.1], city[[54, 3]] <> " (" <> ToString@city[[54, 4]] <> ")"]}

Which is utterly nonsense to the rest of us, who haven't got a clue about what those indices represents. With the dictionary data structure on the other hand we can associate with each object several key/value pairs. For example, to get the population of a certain city we may write:


city["Goteborg"]["Population"]

This is a lot easier to remember than if we had some index for the city and some index for population:


city[[45,3]]

To implement the dictionary data structure, all you have to do is to load the package that I've posted previously here.



<< "~/Documents/Mathematica/datadictionary.m"

Here's an example I've created. It uses tooltips to display the name of a city and its population on mouse over, and it colors each city according to which region it belongs to. It also uses MouseAnnotation to color the cities in the same region as the city you're hovering over white. So it's meant to both exemplify how one can do the things mentioned in the post, and at the same time the dictionary data structure.


getKeys[symbol_] := DownValues[symbol][[All, 1, 1, 1]];

colors = MapIndexed[# -> ColorData[3][First@#2] &, CountryData["Sweden", "Regions"]];

addCity[{name_, region_, country_}] := (
city[name] = makeDictionary[];
dictStore[city[name], "Name", name];

dictStore[city[name], "Region", region];
dictStore[city[name], "Coordinates", CityData[{name, country}, "Coordinates"]];
dictStore[city[name], "Population", CityData[{name, country}, "Population"]];
dictStore[city[name], "Color", region /. colors]
)

addCity /@ CityData[{All, "Sweden"}];

Deploy@Dynamic@Graphics[{
RGBColor[0.896`, 0.8878`, 0.8548`], EdgeForm[GrayLevel[0]],

CountryData["Sweden", "FullPolygon"], {
If[
MouseAnnotation[] === city[#]["Region"],
White,
city[#]["Color"]],
Annotation[
Tooltip[Disk[Reverse@city[#]["Coordinates"], 0.1],
city[#]["Name"] <> " (" <> ToString@city[#]["Population"] <>
")"]
, city[#]["Region"], "Mouse"]

} & /@ getKeys[city]
}, AspectRatio -> Full]

map


As you may have noticed, the first example of how horribly unreadable code with related data may become if you keep the data in a list is just a converted version of the code I'm actually using to draw the disks. Only with the dictionary data structure it looks like this:


{city["Goteborg"]["Color"], Tooltip[Disk[Reverse@city["Goteborg"]["Coordinates"], 0.1], city["Goteborg"]["Name"] <> " (" <> ToString@city["Goteborg"]["Population"] <> ")"]}

Which is much more intuitive.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

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...