python - 用许多数据点绘制散点图会产生模糊的 svg

标签 python svg plotly scatter-plot

当我使用 fig.write_image("filename.svg") 将包含 10000 个数据点的散点图导出为 svg 时 recommended by plotly ,我得到一个模糊的 svg。将数据下采样到 1000 个数据点确实会产生一个保持清晰的漂亮矢量图像。清晰图像和像素化图像之间的限制位于 1000 到 1500 个散点之间,因为下采样到 1500 会再次产生像素化图像。

由于我想导出包含超过 1000 个数据点的数据,如何避免这种情况。

import pandas as pd
import plotly.express as px
import numpy as np
df = pd.DataFrame(np.random.randn(10000, 2), columns=list('XY'))
fig =px.scatter(df, x='X', y='Y')
fig.write_image("images/fig1.svg")

10000 点

screenshot of sharp svg with 10000 datapoints

1500点

screenshot of sharp svg with 1000 datapoints

1000分

screenshot of sharp svg with 1000 datapoints

(我必须制作屏幕截图,因为 stackoverflow 不允许 .svg 图像)

最佳答案

我相信答案是here in the plotly documentation对于 px.scatter...

render_mode (str) – One of 'auto', 'svg' or 'webgl', default 'auto' Controls the browser API used to draw marks. 'svg’ is appropriate for figures of less than 1000 data points, and will allow for fully-vectorized output. 'webgl' is likely necessary for acceptable performance above 1000 points but rasterizes part of the output. 'auto' uses heuristics to choose the mode.

如果您添加 render_mode = 'svg',您应该会看到这种像素化现象消失。

fig =px.scatter(df, x='X', y='Y', render_mode = 'svg')

关于python - 用许多数据点绘制散点图会产生模糊的 svg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71870435/

相关文章:

r - Shiny 更新点击点以突出显示它

Python plotly : use buttons to update sets of subplots & arbitrarily assign traces

python - Slim Docker 镜像包含 git 存储库

python - 在平局的情况下随机选择最大值?

jquery - 单击时使 SVG 可调整大小

r - 修改刻度的对数 Axis

Python:多维字典到数组

python - 为什么 'pip install mysqlclient' 在 ubuntu 18.04 LTS 中不工作

css - 具有嵌入式 CSS 动画的 SVG 在 iOS 10 和 iOS 11 之间表现不同

javascript - d3.js - 使用 Canvas 保持交互