gpt4 book ai didi

python - 我怎样才能绘制ca。 2000 万个点作为散点图?

转载 作者:太空狗 更新时间:2023-10-29 21:17:30 25 4
gpt4 key购买 nike

我正在尝试使用由 ca 组成的 matplotlib 创建一个散点图。约2000万个数据点。即使在最终没有任何可见数据之前将 alpha 值设置为最低,结果也只是一个完全黑色的图。

plt.scatter(timedPlotData, plotData, alpha=0.01, marker='.')

x 轴是大约 2 个月的连续时间轴,y 轴由 150k 连续整数值组成。

有没有办法绘制所有点,以便它们随时间的分布仍然可见?

感谢您的帮助。

最佳答案

有不止一种方法可以做到这一点。很多人建议使用热图/内核密度估计/2d 直方图。 @Bucky 建议使用移动平均线。此外,您可以在移动最小值和移动最大值之间填充,并在顶部绘制移动平均值。我经常称其为“ block 图”,但这是一个糟糕的名字。下面的实现假设您的时间 (x) 值是单调递增的。如果不是,则在 chunkplot 函数中“分 block ”之前按 xy 进行排序就足够简单了。

这里有几个不同的想法。哪个最好取决于你想在情节中强调什么。请注意,这将运行得相当慢,但这主要是由于散点图。其他绘图样式更快。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
np.random.seed(1977)

def main():
x, y = generate_data()
fig, axes = plt.subplots(nrows=3, sharex=True)
for ax in axes.flat:
ax.xaxis_date()
fig.autofmt_xdate()

axes[0].set_title('Scatterplot of all data')
axes[0].scatter(x, y, marker='.')

axes[1].set_title('"Chunk" plot of data')
chunkplot(x, y, chunksize=1000, ax=axes[1],
edgecolor='none', alpha=0.5, color='gray')

axes[2].set_title('Hexbin plot of data')
axes[2].hexbin(x, y)

plt.show()

def generate_data():
# Generate a very noisy but interesting timeseries
x = mdates.drange(dt.datetime(2010, 1, 1), dt.datetime(2013, 9, 1),
dt.timedelta(minutes=10))
num = x.size
y = np.random.random(num) - 0.5
y.cumsum(out=y)
y += 0.5 * y.max() * np.random.random(num)
return x, y

def chunkplot(x, y, chunksize, ax=None, line_kwargs=None, **kwargs):
if ax is None:
ax = plt.gca()
if line_kwargs is None:
line_kwargs = {}
# Wrap the array into a 2D array of chunks, truncating the last chunk if
# chunksize isn't an even divisor of the total size.
# (This part won't use _any_ additional memory)
numchunks = y.size // chunksize
ychunks = y[:chunksize*numchunks].reshape((-1, chunksize))
xchunks = x[:chunksize*numchunks].reshape((-1, chunksize))

# Calculate the max, min, and means of chunksize-element chunks...
max_env = ychunks.max(axis=1)
min_env = ychunks.min(axis=1)
ycenters = ychunks.mean(axis=1)
xcenters = xchunks.mean(axis=1)

# Now plot the bounds and the mean...
fill = ax.fill_between(xcenters, min_env, max_env, **kwargs)
line = ax.plot(xcenters, ycenters, **line_kwargs)[0]
return fill, line

main()

enter image description here

关于python - 我怎样才能绘制ca。 2000 万个点作为散点图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18879267/

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