gpt4 book ai didi

python - 大量数据的散点图

转载 作者:太空狗 更新时间:2023-10-30 02:39:12 24 4
gpt4 key购买 nike

假设我有一个大型数据集 (8500000X50)。我想散点图 X(日期)和 Y(在某一天进行的测量)。

我只能得到这个: enter image description here

data_X = data['date_local']
data_Y = data['arithmetic_mean']
data_Y = data_Y.round(1)
data_Y = data_Y.astype(int)
data_X = data_X.astype(int)
sns.regplot(data_X, data_Y, data=data)
plt.show()

根据我在 Stackoverflow 上发现的某种“相同”问题,我可以洗牌我的数据或取 1000 个随机值并绘制它们。但是如何以每个 X(进行特定测量的日期)对应于实际(Y 测量)的方式实现它。

最佳答案

首先回答你的问题:

你应该使用 pandas.DataFrame.sample从你的日期框架中获取样本,然后使用 regplot ,下面是一个使用随机数据的小例子:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import numpy as np
import pandas as pd
import seaborn as sns

dates = pd.date_range('20080101', periods=10000, freq="D")
df = pd.DataFrame({"dates": dates, "data": np.random.randn(10000)})

dfSample = df.sample(1000) # This is the importante line
xdataSample, ydataSample = dfSample["dates"], dfSample["data"]

sns.regplot(x=mdates.date2num(xdataSample.astype(datetime)), y=ydataSample)
plt.show()

regplot 上,由于日期时间的类型,我在我的 X 数据中执行了转换,请注意,根据您的数据,这肯定不需要

所以,不是这样的:

你会得到这样的东西:


现在,一个建议:

使用sns.jointplot ,它有一个 kind 参数,来自 docs:

kind : { “scatter” | “reg” | “resid” | “kde” | “hex” }, optional

Kind of plot to draw.

我们在这里创建的与 matplotlib 的 hist2d 所做的类似,它使用您的整个数据集创建类似于热图的东西。使用随机数据的示例:

dates = pd.date_range('20080101', periods=10000, freq="D")
df = pd.DataFrame({"dates": dates, "data": np.random.randn(10000)})

xdata, ydata = df["dates"], df["data"]
sns.jointplot(x=mdates.date2num(xdata.astype(datetime)), y=ydata, kind="kde")

plt.show()

这会产生这张图片,这也有助于查看沿所需轴的分布:

关于python - 大量数据的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45092124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com