大数据散点图添加线条

发布于 2025-01-19 03:25:31 字数 2119 浏览 1 评论 0原文

我需要一个包含 77M 以上行的数据集的散点图,并添加像 plt.axlines 这样的行。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({ 'x' :np.random.normal(0,1,77000000),
                    'y' : np.random.normal(0,1,77000000) })

intercept_a = (-2, 0.5)
slope_a     = 0.5

intercept_b = (-0.5, -1.5)
slope_b     = 0.1

print(df.shape) # (77000000, 2)

预期结果

# (but w/ 77M rows)

plt.scatter(x = df.loc[1:3000,'x'], 
            y = df.loc[1:3000,'y'])
plt.axline(intercept_a, slope=slope_a, color='red', label='dry')
plt.axline(intercept_b, slope=slope_b, color='blue', label='edge')

在此处输入图像描述

我尝试过使用 datashader 和 vaex,但未成功

Datashader

# DATASHADER 
import datashader as ds

cvs = ds.Canvas(plot_width=800, plot_height=500)  # auto range or provide the `bounds` argument
agg = cvs.points(df, 'x', 'y')
img = ds.tf.set_background(ds.tf.shade(agg, cmap=cc.fire), "white").to_pil()
plt.axline(intercept_a, slope=slope_a, color='red', label='dry')
plt.axline(intercept_b, slope=slope_b, color='blue', label='edge')
plt.imshow(img)

在此处输入图像描述

Vaex

# VAEX
import vaex
df_vaex = vaex.from_pandas(df=df)

df_vaex.viz.heatmap("x", "y",
               limits = '97%',
               figsize=(15, 7),
               colorbar = False);

plt.axline(intercept_a, slope=slope_a, color='red', label='dry')
plt.axline(intercept_b, slope=slope_b, color='blue', label='edge')

在此处输入图像描述

PS 此处是我的真实数据的数据着色器图,我需要背景白色才能很好地可视化饱和点。

输入图片此处描述

I need a scatterplot for a dataset with 77M+ rows, plus adding lines like the plt.axlines.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({ 'x' :np.random.normal(0,1,77000000),
                    'y' : np.random.normal(0,1,77000000) })

intercept_a = (-2, 0.5)
slope_a     = 0.5

intercept_b = (-0.5, -1.5)
slope_b     = 0.1

print(df.shape) # (77000000, 2)

Expected result

# (but w/ 77M rows)

plt.scatter(x = df.loc[1:3000,'x'], 
            y = df.loc[1:3000,'y'])
plt.axline(intercept_a, slope=slope_a, color='red', label='dry')
plt.axline(intercept_b, slope=slope_b, color='blue', label='edge')

enter image description here

I have tried with datashader and vaex unsuccesfully

Datashader

# DATASHADER 
import datashader as ds

cvs = ds.Canvas(plot_width=800, plot_height=500)  # auto range or provide the `bounds` argument
agg = cvs.points(df, 'x', 'y')
img = ds.tf.set_background(ds.tf.shade(agg, cmap=cc.fire), "white").to_pil()
plt.axline(intercept_a, slope=slope_a, color='red', label='dry')
plt.axline(intercept_b, slope=slope_b, color='blue', label='edge')
plt.imshow(img)

enter image description here

Vaex

# VAEX
import vaex
df_vaex = vaex.from_pandas(df=df)

df_vaex.viz.heatmap("x", "y",
               limits = '97%',
               figsize=(15, 7),
               colorbar = False);

plt.axline(intercept_a, slope=slope_a, color='red', label='dry')
plt.axline(intercept_b, slope=slope_b, color='blue', label='edge')

enter image description here

PS here is the datashader plot for my real data, I need the background white for a good visualization of the saturated points.

enter image description here

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文