如何在 ListPlots 中注释多个数据集 [英] How to annotate multiple datasets in ListPlots

查看:29
本文介绍了如何在 ListPlots 中注释多个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要同时可视化多个数据集,通常是在 ListPlot 或其 Log-companions 中.由于数据集的数量通常大于易于区分的线条样式的数量,并且创建大型绘图图例仍然有些不直观,我仍在寻找一种好方法来注释我的绘图中的不同线条/集.在屏幕上工作时,工具提示很好,但如果我需要打印情节,它们无济于事.

Frequently I have to visualize multiple datasets simultaneously, usually in ListPlot or its Log-companions. Since the number of datasets is usually larger than the number of easily distinguishable line styles and creating large plot legends is still somewhat unintuitiv I am still searching for a good way to annotate the different lines/sets in my plots. Tooltip is nice when working on screen, but they don't help if I need to pritn the plot.

最近,我使用 Mesh 选项来枚举我的数据集,发现了一些奇怪的东西

Recently, I played around with the Mesh option to enumerate my datasets and found some weird stuff

GraphicsGrid[Partition[Table[ListPlot[
Transpose@
 Table[{Sin[x], Cos[x], Tan[x], Cot[x]}, {x, 0.01, 10, 0.1}], 
PlotMarkers -> {"1", "2", "3", "4"}, Mesh -> i, Joined -> True, 
PlotLabel -> "Mesh\[Rule]" <> ToString[i], ImageSize -> 180], {i, 
1, 30}], 4]]

结果在我的机器上看起来像这样(Windows 7 x64,Mathematica 8.0.1):

The result looks like this on my machine (Windows 7 x64, Mathematica 8.0.1):

有趣的是,对于 Mesh->2、8 和 10,结果看起来像我预期的那样,其余的则不然.要么我不理解 Mesh 选项,要么它不理解我.

Funnily, for Mesh->2, 8 , and 10 the result looks like I expected it, the rest does not. Either I don't understand the Mesh option, or it doesn't understand me.

这是我的问题:

  1. ListPLot 中的 Mesh 是不是有问题,还是我使用不当?
  2. 如何对连续组的网格点进行 X 移位以避免叠印?
  3. 对于如何在图中注释/枚举多个数据集,您还有其他建议吗?

推荐答案

您可以按照这些思路尝试一些事情.将每一行变成一个按钮,当点击时,可以识别自己.

You could try something along these lines. Make each line into a button which, when clicked, identifies itself.

plot=Plot[{Sin[x],Cos[x]},{x,0,2*Pi}];
sinline=plot[[1,1,3,2]];
cosline=plot[[1,1,4,2]];
message="";
altplot=Append[plot,PlotLabel->Dynamic[message]];
altplot[[1,1,3,2]]=Button[sinline,message="Clicked on the Sin line"];
altplot[[1,1,4,2]]=Button[cosline,message="Clicked on the Cos line"];
altplot

如果您添加一个 EventHandler,您可以获得您单击的位置并将带有相关定位标签的 Inset 添加到绘图中.将绘图包装在 Dynamic 中,以便在每次单击按钮后自动更新.它工作正常.

If you add an EventHandler you can get the location where you clicked and add an Inset with the relevant positioned label to the plot. Wrap the plot in a Dynamic so it updates itself after each button click. It works fine.

为了回应评论,这里有一个更完整的版本:

In response to comments, here is a fuller version:

plot = Plot[{Sin[x], Cos[x]}, {x, 0, 2*Pi}];
sinline = plot[[1, 1, 3, 2]];
cosline = plot[[1, 1, 4, 2]];
AddLabel[label_] := (AppendTo[plot[[1]],
    Inset[Framed[label, Background -> White], pt]];
   (* Remove buttons for final plot *)
   plainplot = plot;
   plainplot[[1, 1, 3, 2]] = plainplot[[1, 1, 3, 2, 1]];
   plainplot[[1, 1, 4, 2]] = plainplot[[1, 1, 4, 2, 1]]);
plot[[1, 1, 3, 2]] = Button[sinline, AddLabel["Sin"]];
plot[[1, 1, 4, 2]] = Button[cosline, AddLabel["Cos"]];
Dynamic[EventHandler[plot,
  "MouseDown" :> (pt = MousePosition["Graphics"])]]

要添加标签,请单击该行.最终带注释的图表设置为plainplot",可打印和复制,并且不包含动态元素.

To add a label click on the line. The final annotated chart, set to 'plainplot', is printable and copyable, and contains no dynamic elements.

[当天晚些时候] 另一个版本,这次是通用的,基于初始图表.(使用了 Mark McClure 的部分解决方案.)对于不同的图,可以根据需要编辑ff"和spec".

[Later in the day] Another version, this time generic, and based on the initial chart. (With parts of Mark McClure's solution used.) For different plots 'ff' and 'spec' can be edited as desired.

ff = {Sin, Cos, Tan, Cot};
spec = Range[0.1, 10, 0.1];
(* Plot functions separately to obtain line counts *)
plots = Array[ListLinePlot[ff[[#]] /@ spec] &, Length@ff];
plots = DeleteCases[plots, Line[_?(Length[#] < 3 &)], Infinity];
numlines = Array[Length@Cases[plots[[#]], Line[_], Infinity] &,
   Length@ff];
(* Plot functions together for annotation plot *)
plot = ListLinePlot[#@spec & /@ ff];
plot = DeleteCases[plot, Line[_?(Length[#] < 3 &)], Infinity];
lbl = Flatten@Array[ConstantArray[ToString@ff[[#]],
      numlines[[#]]] &, Length@ff];
(* Line positions to substitute with buttons *)
linepos = Position[plot, Line, Infinity];
Clear[line];
(* Copy all the lines to line[n] *)
Array[(line[#] = plot[[Sequence @@ Most@linepos[[#]]]]) &,
  Total@numlines];
(* Button function *)
AddLabel[label_] := (AppendTo[plot[[1]],
    Inset[Framed[label, Background -> White], pt]];
   (* Remove buttons for final plain plot *)
   plainplot = plot;
   bpos = Position[plainplot, Button, Infinity];
   Array[(plainplot[[Sequence @@ Most@bpos[[#]]]] =
       plainplot[[Sequence @@ Append[Most@bpos[[#]], 1]]]) &,
    Length@bpos]);
(* Substitute all the lines with line buttons *)
Array[(plot[[Sequence @@ Most@linepos[[#]]]] = Button[line[#],
      AddLabel[lbl[[#]]]]) &, Total@numlines];
Dynamic[EventHandler[plot,
  "MouseDown" :> (pt = MousePosition["Graphics"])]]

这是它的外观.注释后,可以发现普通图形对象设置为 'plainplot' 变量.

Here's how it looks. After annotation the plain graphics object can be found set to the 'plainplot' variable.

这篇关于如何在 ListPlots 中注释多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆