Mathematica 中的 ListPlot、ErrorListPlot 中的单个点着色

发布于 2024-12-23 08:54:33 字数 1208 浏览 2 评论 0原文

我可以通过执行类似

ListLinePlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False]

Mathematicagraphics

的操作来获得彩色 ListLinePlot但是,如帮助所示文件(“ColorFunction 需要至少一个数据集加入”),如果我执行等效的

ListPlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False]

Mathematicagraphics

我的所有点都是蓝色的。有没有一种好的方法可以让 ColorFunctionJoined ->; 一起用于 ListPlot ?假的?

也就是说,有没有更好的方法来获得类似的东西

ListPlot[
 List /@ Transpose[{Range[(680 - 420)/20 + 1], Range[420, 680, 20]}], 
 PlotMarkers -> ({Graphics[{#, Disk[]}], 0.05} & /@ ColorData["VisibleSpectrum"] /@ Range[420, 680, 20])
]

Mathematicagraphics

(另外,有没有人能解释为什么 Mathematica 需要加入 - > True 以便使用 ColorFunction?)

编辑:我也在寻找一种方法来使用 ErrorListPlot 进行类似的着色ErrorBarPlots 包。

I can get a colored ListLinePlot by doing something like

ListLinePlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False]

Mathematica graphics

However, as indicated by the help file ("ColorFunction requires at least one dataset to be Joined"), if I do the equivalent

ListPlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False]

Mathematica graphics

all my points are blue. Is there a nice way to get ColorFunction to work for ListPlot with Joined -> False?

That is, is there a nicer way to get something like

ListPlot[
 List /@ Transpose[{Range[(680 - 420)/20 + 1], Range[420, 680, 20]}], 
 PlotMarkers -> ({Graphics[{#, Disk[]}], 0.05} & /@ ColorData["VisibleSpectrum"] /@ Range[420, 680, 20])
]

?

Mathematica graphics

(Also, does anyone have an explanation of why Mathematica requires Joined -> True in order to make use of ColorFunction?)

Edit: I'm also looking for a way to do a similar coloring with ErrorListPlot in the ErrorBarPlots package.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-12-30 08:54:33

问题是,Joined->True 绘制一条 Line[],可以为每个包含点指定 VertexColors。我假设在设置 Joined->False 时对点执行相同的操作会导致它不起作用的情况。不过,在您的情况下,Line[] 和 Point[] 的工作原理几乎相同。 又是什么呢?

ListLinePlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", 
  ColorFunctionScaling -> False] /. Line[arg___] :> Point[arg]

那么Mathematicagraphics

顺便说一句,如果您仅使用 ListLinePlot,则唯一的 Line[] 指令产生的是您的数据中的数据,即使您有更多数据集和 {x,y} 坐标,这也应该有效

data = Transpose[Table[{{x, Sin[x]}, {x, Cos[x]}}, {x, 0, 2 Pi, 0.2}]];
ListLinePlot[data, ColorFunction -> Hue] /. Line[arg___] :> Point[arg]

Mathematicagraphics

The problem is, that Joined->True draws a Line[] which can be given VertexColors for each containing point. I assume doing the same for the points when setting Joined->False leads to situations where it does not work. Nevertheless, Line[] and Point[] work pretty much the same in your case. So what is about

ListLinePlot[Range[420, 680, 20], ColorFunction -> "VisibleSpectrum", 
  ColorFunctionScaling -> False] /. Line[arg___] :> Point[arg]

Mathematica graphics

And, by the way, if your using a ListLinePlot only, where the only Line[] directives arising are the one from your data, this should work even if you have more datasets and {x,y} coordinates

data = Transpose[Table[{{x, Sin[x]}, {x, Cos[x]}}, {x, 0, 2 Pi, 0.2}]];
ListLinePlot[data, ColorFunction -> Hue] /. Line[arg___] :> Point[arg]

Mathematica graphics

小傻瓜 2024-12-30 08:54:33

您可以使用 DiscretePlot

data = Range[420, 680, 20];
DiscretePlot[data[[i]], {i, Length[data]},
   ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False,
   Filling -> None]

Mathematicagraphics

如果您要绘制 x,y 点列表,它变得有点棘手:

data = Transpose[{Range[420, 680, 20], Range[400, 530, 10]}];
mapping = Apply[Rule, data, 2];
DiscretePlot[i/.mapping, {i, data[[;;,1]]},
   ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False,
   Filling -> None]

Mathematicagraphics

DiscretePlot 确实看起来很奇怪让您对点进行不同的着色,而 ListPlot 不会。我确信这一定与实现细节有关,但我想不出为什么会出现这种情况。

You can use DiscretePlot:

data = Range[420, 680, 20];
DiscretePlot[data[[i]], {i, Length[data]},
   ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False,
   Filling -> None]

Mathematica graphics

If you're plotting a list of x,y points, it gets a little trickier:

data = Transpose[{Range[420, 680, 20], Range[400, 530, 10]}];
mapping = Apply[Rule, data, 2];
DiscretePlot[i/.mapping, {i, data[[;;,1]]},
   ColorFunction -> "VisibleSpectrum", ColorFunctionScaling -> False,
   Filling -> None]

Mathematica graphics

It does seem rather odd that DiscretePlot will let you color the points differently whereas ListPlot won't. I'm sure it must have something to do with the implementation details, but I can't think of a reason why that would be the case.

这样的小城市 2024-12-30 08:54:33

我在工作中也遇到了这个问题。我按以下方式为每个点分配颜色:

data = ...
ListPlot[data] /. Point[args___] :> Point[args, VertexColors -> {c1, c2, ...}]

其中 c1 是第一个数据点的颜色,依此类推。颜色列表可以通过编程方式生成,例如

ColorData["Rainbow"] /@ (Range@Length@data / Length@data)

这是结果。

这样做的优点方法如下。

  • 很简单:我们有一个配对列表,然后创建一个相应的颜色列表。
  • 我们原来的ListPlot代码不需要修改(例如,改为ListLinePlot)。

I came across this problem in my work too. I assign a colour to each point in the following manner:

data = ...
ListPlot[data] /. Point[args___] :> Point[args, VertexColors -> {c1, c2, ...}]

where c1 is the colour for the first data point, and so on. The colour list may be programmatically generated, eg

ColorData["Rainbow"] /@ (Range@Length@data / Length@data)

Here is the result.

The good points of this method are as follows.

  • It's straightforward: we have a list of pairs, then we create a corresponding list of colours.
  • Our original ListPlot code needs not be modified (eg, changed into ListLinePlot).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文