plotly:大量数据点

问题描述 投票:0回答:4

我正在尝试使用plotly 绘制具有大量数据点(2mm-3mm)的内容。

当我跑步时

py.iplot(fig, filename='test plot')

我收到以下错误:

Woah there! Look at all those points! Due to browser limitations, the Plotly SVG drawing functions have a hard time graphing more than 500k data points for line charts, or 40k points for other types of charts. Here are some suggestions:
(1) Use the `plotly.graph_objs.Scattergl` trace object to generate a WebGl graph.
(2) Trying using the image API to return an image instead of a graph URL
(3) Use matplotlib
(4) See if you can create your visualization with fewer data points

If the visualization you're using aggregates points (e.g., box plot, histogram, etc.) you can disregard this warning.

然后我尝试用这个来保存它:

py.image.save_as(fig, 'my_plot.png')

但是后来我得到了这个错误:

PlotlyRequestError: Unknown Image Server Error

我该如何正确地做到这一点?我不在乎它是静态图像还是笔记本电脑中的交互式显示。

python plotly
4个回答
11
投票

Plotly 在这方面似乎真的很糟糕。我只是想创建一个包含 500 万个点的箱线图,这在简单的 R 函数“箱线图”中没有问题,但绘图为此无休止地计算。

改善这一点应该是一个重大问题。并非所有数据都必须保存(并显示)在绘图对象中。我认为这是主要问题。


8
投票

一个选择是对数据进行下采样,不确定您是否愿意: https://github.com/devoxi/lttb-py

我在浏览器中处理大型数据集时也遇到问题 - 如果有人有解决方案,请写信! 谢谢!


0
投票

您可以尝试

render_mode
论证。示例:

import plotly.express as px
import pandas as pd
import numpy as np

N = int(1e6) # Number of points

df = pd.DataFrame(dict(x=np.random.randn(N),
                       y=np.random.randn(N)))

fig = px.scatter(df, x="x", y="y", render_mode='webgl')
fig.update_traces(marker_line=dict(width=1, color='DarkSlateGray'))
fig.show()

在我的电脑中

N=1e6
大约需要5秒才能看到剧情,“互动性”还是很好的。使用
N=10e6
大约需要 1 分钟,并且绘图不再响应(即缩放、平移或其他任何操作都非常慢)。


0
投票

使用WebGL渲染模式。我有一个大约 500k 点的图表,如果我使用 SVG,在浏览器中速度会非常慢。通过更改为 WebGL,它就像一个魅力。

您可以在这里找到一些如何在plotly中使用WebGL的示例:

https://plotly.com/python/webgl-vs-svg/

© www.soinside.com 2019 - 2024. All rights reserved.