Plotly:绘制 (n) 个点时标记消失 [英] Plotly: Markers disappear when (n) points are plotted

查看:55
本文介绍了Plotly:绘制 (n) 个点时标记消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我最初的想法是在 plotly 中绘制线图,并在特定阈值 t 之后用一种颜色为线着色,在阈值之前用另一种颜色着色.它适用于 23 或更少的点,但它的工作没有更多,使用这种方法:

Okay, so my initial idea is to make a line plot in plotly and color the line with one color after certain threshold t, and another color before the threshold. It works for a 23 or less points, but it works with no more, using this method:

import numpy as np
import plotly.graph_objects as go

X = [j for j in range(0, 100)]
Y = [j for j in range(100000, 200000, 1000)]
X = X[:23]
Y = Y[:23]
X = np.array(X)
Y = np.array(Y)
t = 4

x = X[X <= t]  # Include the threshold
y = Y[X <= t]
bx = X[X >= t]
by = Y[X >= t]

fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, line=dict(width=4, color='grey'), name="useless data"))
fig.add_trace(go.Scatter(x=bx, y=by, line=dict(width=4, color='blue'), name="useful data"))
fig.update_layout(xaxis_title="x axis", yaxis_title="y axis")
fig.show()

所以这正常工作,如果你运行它,你会看到4个包含在蓝点中.但是现在,请删除仅取 23 个值的行 (X = X[:23], Y = Y[:23]).你会看到4不再是蓝点的一部分,而且点本身从图中的蓝线消失了,你可以悬停看到数据,但你看不到实际的点!如果有人知道为什么会发生这种情况,它是一个实际的错误还是正常行为并且我遗漏了什么?提前致谢!

So this works normally, and if you run it, you will see that 4 is included in the blue points. But now, please remove the lines where only 23 values are taken (X = X[:23], Y = Y[:23]). You will see that 4 is no longer part of the blue points, moreover, the points themselves disappear from the graph in the blue line, you can hover and see data, but you can't see the actual points! If anyone knows why this happens, is it an actual bug or it is normal behaviour and there is something I am missing? Thank you in advance!

推荐答案

Bug?不必要.奇怪的行为?也许……

无论如何,您的解决方案是:

Bug? Not necessarily. Weird behaviour? Perhaps...

In any case, the solution in your case is:

fig.data[1].mode = 'lines+markers'


你似乎在这里挣扎是由两件事造成的:


What you seem to be struggling with here is caused by two things:

  1. 当悬停在一个点上,其中一条轨迹由一条线表示,一条轨迹由一个标记表示,即使该线位于顶部,plotly 也会显示该标记的信息.
  2. 对于增加 go.Scatter() 轨迹的长度,在达到特定阈值后,plotly 将停止显示标记.
  3. 这也许是可以说是奇怪的部分;确切的阈值似乎不仅仅由轨迹的长度决定.我们会在最后看一看.
  1. When hovering over a point where there is one trace represented by a line, and one trace represented by a marker, plotly will display the information for the marker even though the line is placed on top.
  2. For an increasing length of a go.Scatter() trace, plotly will stop showing markers for after a certain threshold.
  3. And this is perhaps the arguably weird part; that the exact threshold does not seem to be determined by the length of the trace alone. We'll take a look at that in the end.


详情:


1.悬停行为

只需按原样运行您的代码,然后将鼠标悬停在 4 上:

现在通过单击图例中的名称取消选择无用数据,您将获得:

Now deselect useless data by clicking the name in the legend and you'll get:

如果你放大一点,你会看到数据实际上就在那里,只是当两条轨迹都被激活时它不会在悬停时显示:

If you zoom in a bit, you'll see that the data is actually there, it just won't show on hover when both traces are activated:

只包括:

fig.data[1].mode = 'lines+markers'

并得到:

在您的情况下,此阈值似乎是长度为 23 的跟踪,因为您看到的是您所描述的确切行为.那么,这有什么奇怪的呢?下一部分:

In your case, this threshold seems to be a trace with length = 23 since you're seeing the exact behaviour you're describing. So, what's weird about this? The next part:

首先,*为什么有门槛?可能是因为带有太多标记的轨迹看起来很奇怪:

First of all, *why is there a threshold? Probably because a trace with too many markers arguably looks weird:

您发现阈值是 24.但是在仅使用 go.Figure(go.Scatter(x = x, y = y)) 构建的图形中,阈值是 20:

You found the threshold to be 24. But in a figure built only with go.Figure(go.Scatter(x = x, y = y)), the threshold is 20:

pts = 20
x = np.arange(1,pts)
y = np.arange(1,pts)
fig = go.Figure(go.Scatter(x = x, y = y)).show()

pts = 21
x = np.arange(1,pts)
y = np.arange(1,pts)
fig = go.Figure(go.Scatter(x = x, y = y)).show()

而且我不确定为什么.但我认为这本身就是一个很好的问题.

And I'm not sure why. But I think that would make for a good question on its own.

这篇关于Plotly:绘制 (n) 个点时标记消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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