Fro*_*SHI 7 python numpy matplotlib
我正在尝试使用matplotlib创建一个散点图.约 2000万个数据点.即使在最终没有可见数据之前将alpha值设置为最低值之后,结果也只是一个完全黑色的图.
plt.scatter(timedPlotData, plotData, alpha=0.01, marker='.')
Run Code Online (Sandbox Code Playgroud)
x轴是约2个月的连续时间线,y轴由150k个连续的整数值组成.
有没有办法绘制所有点,以便它们随时间的分布仍然可见?
谢谢您的帮助.
Joe*_*ton 14
这样做的方法不止一种.很多人建议使用热图/核密度估计/ 2d直方图.@Bucky建议使用移动平均线.此外,您可以在移动最小值和移动最大值之间填充,并在顶部绘制移动平均值.我经常把它称为"块状图",但这是一个可怕的名字.下面的实现假设您的时间(x)值单调递增.如果他们没有,这是很简单的排序,y通过x在"分块"之前chunkplot的功能.
这里有几个不同的想法.哪个最好取决于你想要在情节中强调什么.请注意,这将是相当慢的运行,但这主要是由于散点图.其他绘图风格要快得多.
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()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
3692 次 |
| 最近记录: |