1
$\begingroup$

I have the following values:

DataACT1;(* Being a list of countries - a column in my dataset*)
DataACT2; (* Being a list of happiness scores - a column in my dataset*)
DataACT3; (*List of GDPs - a column in my dataset*)

Then I combined DataACT2 and DataACT3 to generate a listplot:

    DataACT2Combination = Transpose[{DataACT2, DataACT3}];
ListPlot [Tooltip [DataACT2Combination, DataACT1],AxesLabel -> {"Happiness Score", "GDP (per capita)"},PlotLabel -> "GDP (per capita) and Happiness Score",PlotRange -> {{1, 10}, {1, 120000}} ] // Framed

Which gives me to this Plot, with the respective tooltip when going over the dots with the mouse:

enter image description here

The problem is that I want the tooltip to show me just one country per dot (the respective entry of DataACT1 for the combination of the respective DataACT2Combination. Right now Mathematica is giving me the entire entry of DataACT1. How can I change this?

$\endgroup$
4
$\begingroup$

You should generate a single tooltip for each country; Tooltip will not automatically thread over the list.

Let's generate some data:

SeedRandom[20200106]
listOfCountries = 
  WolframAlpha["countries world", {{"Result", 1}, "Output"}, 
    InputAssumptions -> {"*MC.%7E-_*DataType-"}, 
    PodStates -> {"Result__More", "Result__More"}] // ReleaseHold;
happiness = RandomInteger[{0, 10}, Length[listOfCountries]];
gdp = RandomReal[{0, 140000}, Length[listOfCountries]];

Then generate the pairs (Transpose) and associate the name of the country with its data (MapThreading an appropriate Tooltip):

annotated = MapThread[
    Tooltip[#1, #2] &,
    {Transpose@{happiness, gdp}, listOfCountries}
 ];
ListPlot[annotated]

This will generate a plot with each dot annotated with the name of the country it represents:

plot with a tooltip showing a single country name


You could also form an association from your data and then let ListPlot deal with the annotations itself: it will generate some labels as well as all the tooltips you seek. This is perhaps more direct, but I find that it gives you less control on the presentation:

ListPlot@AssociationThread[listOfCountries -> Transpose@{happiness, gdp}]

plot with tooltips and annotations

$\endgroup$
  • $\begingroup$ Thank you so much! This worked great! $\endgroup$ – Faye Drew Jan 7 at 14:49
  • $\begingroup$ @Faye you're very welcome! Glad it helped! $\endgroup$ – MarcoB Jan 7 at 15:38

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.