Message Boards Message Boards

4
|
4220 Views
|
8 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Plot with filling and how to get the tooltip on the filling?

Here is a sample piece of code that generates a ListPlot with fillings between the lines:

StackedListPlot[{Tooltip[{0, 5, 1, 0, 4, 3, 4, 9, 0, 9}, "First"], 
  Tooltip[{0, 0, 2, 0, 0, 0, 0, 0, 0, 0}, "Second"], 
  Tooltip[{2, 7, 3, 9, 0, 6, 4, 1, 4, 3}, "Third"], 
  Tooltip[{3, 3, 2, 2, 0, 0, 0, 4, 3, 5}, "Fourth"]}]

sample list plot

If you run the code and move the mouse cursor on the lines, you get a tooltip. I would like to get the tooltip also when the mouse is on the fillings between the lines. Any suggestions?

POSTED BY: Luc Barthelet
8 Replies
Posted 4 years ago

Hi Luc,

This method can be brittle. Can you elaborate on "but it failed"? If there are additional GraphicsGroup wrapping them in Tooltip should not break anything, at worst there would be unintended tooltips (which would provide a clue). Could you also post all of the options you are passing to StackedListPlot? I tried to reproduce your problem with 64 lines and no additional options and it worked fine. It must be the additional options that are causing the issue. Thanks.

SeedRandom[1234];
data = RandomInteger[9, {64, 10}];
plot = StackedListPlot[data, ImageSize -> Large];
groups = Cases[plot, _GraphicsGroup, Infinity];
groupPositions = Position[plot, _GraphicsGroup];
labels = IntegerName[#, "Ordinal"] & /@ Range[Length@groups];
tooltipGroups = MapThread[Tooltip, {groups, labels}];
ReplacePart[plot, Thread[groupPositions -> tooltipGroups]]

enter image description here

POSTED BY: Rohit Namjoshi

@Rohit Namjoshi : I definitely like your idea of using the internals of StackedListPlot!

Just as a remark: In case that all data are >0, after the preliminaries

data = RandomInteger[{1, 9}, {20, 10}]; (* all numbers have to be >0 !!! *)
plot = StackedListPlot[data, ImageSize -> Large];
labels = IntegerName[#, "Ordinal"] & /@ Range[Length@data];

the code can be simplified:

n = 1;
plot /. GraphicsGroup[a_] :> Tooltip[GraphicsGroup[a], labels[[n++]]]
POSTED BY: Henrik Schachner

If you change the

labels = IntegerName[#, "Ordinal"] & /@ Range[Length@groups];

into what should be more properly:

labels = IntegerName[#, "Ordinal"] & /@ Range[Length@data];

You will find example that fails, because there are more groups than actual data sets.

Here is a set that fails: (maybe you can figure out why...)

data =
  {{58, 58, 41, 40, 37, 36, 36, 36, 36, 37, 36, 33, 32, 33, 33, 35, 
    35, 35, 53, 53, 53, 53, 53, 53}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 2, 6, 6, 6, 6, 6, 3, 
    4, 3, 3, 5, 4, 5, 5, 6, 4, 4, 4, 4, 5, 4, 4, 4}, {0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 
    0, 0, 0, 0, 0, 0, 0, 2, 1, 5, 6, 4, 2, 1, 3, 1, 1, 1, 0, 0, 0, 
    0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 0, 0, 0, 0, 0}, {0, 0, 3, 3, 3, 4, 5, 7, 8, 7, 8, 9, 10, 9, 
    10, 9, 10, 11, 11, 10, 10, 10, 10, 11}, {0, 0, 1, 1, 1, 1, 1, 1, 
    1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 
    0, 0, 0, 0, 7, 7, 7, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0}, {0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1}, {0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 3, 3, 3, 0, 0, 3, 3, 3, 0, 0, 
    0, 0, 0, 0}, {15, 15, 15, 15, 15, 15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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 3, 3}, {2, 2, 2, 2, 2, 2, 2, 1, 
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 5, 7, 7, 2, 2, 2, 2, 2, 2}, {0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 
    1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3, 3, 3, 
    3, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 
    6, 6, 4, 5, 5, 4, 5, 5, 2, 3}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 4, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 
    0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
POSTED BY: Luc Barthelet

Yes, it fails because your data are not >0, unfortunately. Then the number of disjoint areas in the plot exceeds the number of data lines and one has to do it the way Rohit proposed - this gives a result, but - as said - with more areas then data lines.

POSTED BY: Henrik Schachner
Posted 4 years ago

Hi Luc,

Another way to do it is to modify the expression corresponding to the generated plot. The symbolic representation may change between WL versions and will have to be adjusted accordingly.

plot = StackedListPlot[{
    Tooltip[{0, 5, 1, 0, 4, 3, 4, 9, 0, 9}, "First"],
    Tooltip[{0, 0, 2, 0, 0, 0, 0, 0, 0, 0}, "Second"],
    Tooltip[{2, 7, 3, 9, 0, 6, 4, 1, 4, 3}, "Third"],
    Tooltip[{3, 3, 2, 2, 0, 0, 0, 4, 3, 5}, "Fourth"]}];

(* Shaded regions are GraphicsGroup, extract them and their positions *)
groups = Cases[plot, _GraphicsGroup, Infinity];
groupPositions = Position[plot, _GraphicsGroup];

(* Wrap Tooltip around each GraphicGroup *)
labels = {"First", "Second", "Third", "Fourth"};
tooltipGroups = MapThread[Tooltip, {groups, labels}];

(* Replace GraphicsGroup with the Tooltip wrapped form*)
ReplacePart[plot, Thread[groupPositions -> tooltipGroups]]

enter image description here

POSTED BY: Rohit Namjoshi

Thank you! Very elegant.

I tried to use this, but it failed with my actual data set which is a bit more complex than this. It has 64 data lines, but somehow the plot generates 66 _GraphicsGroup. I am worried also that as I use more features this will be an even bigger problem.

POSTED BY: Luc Barthelet

Hi Luc,

Maybe there is some more elegant way - one possibility is to work with Graphics and Polygons instead of ListPlot and friends:

(* starting with your input: *)
data = {Tooltip[{0, 5, 1, 0, 4, 3, 4, 9, 0, 9}, "First"], 
    Tooltip[{0, 0, 2, 0, 0, 0, 0, 0, 0, 0}, "Second"], 
    Tooltip[{2, 7, 3, 9, 0, 6, 4, 1, 4, 3}, "Third"], 
    Tooltip[{3, 3, 2, 2, 0, 0, 0, 4, 3, 5}, "Fourth"]}[[All, 1]];
stackedData = Transpose[Accumulate /@ Transpose[data]];
polyData = MapIndexed[{First[#2], #1} &] /@ stackedData;
polyData = Join[{{1, 0}}, #, {{10, 0}}] & /@ polyData;
polygs = {EdgeForm[Thick], RandomColor[], Polygon[#]} & /@ polyData;
tPolygs = Reverse@Thread[Tooltip[polygs, {"First", "Second", "Third", "Forth"}]];
Graphics[tPolygs, AspectRatio -> 1/2, Frame -> True, ImageSize -> Large]

Does that help? Regards -- Henrik

POSTED BY: Henrik Schachner

Thank you. I will most likely use this and rebuild my own graph completely...

POSTED BY: Luc Barthelet
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract