3

我想用本文图 5 中给出的颜色图绘制堆栈图。这是相同的屏幕截图

在此处输入图像描述

目前,我能够绘制类似性质的散点图。

在此处输入图像描述

我想将此散点图转换为带有颜色图的堆栈图。我有点迷失了这样做的想法。我最初的猜测是,对于每个 (x,y) 点,我需要颜色图谱上的 z 点列表。但是,我想知道是否有更简单的方法。这是我用彩色图生成散点图的代码

cm = plt.cm.get_cmap('RdYlBu')
plt.xscale('log')
plt.yscale('log')
sc = plt.scatter(x, y, c=z, marker ='x', norm = matplotlib.colors.Normalize(vmin= np.min(z), vmax=np.max(z)), s=35, cmap=cm)
plt.colorbar(sc)
plt.show()

编辑

我觉得我需要找到一种方法将其转换z-array为多个z-arrays- 一个用于颜色条上的每个 bin。然后我可以简单地从这些派生的z-arrays.

编辑 2

我遵循了Rutger 的代码,并且能够为我的数据生成这个图表。我想知道为什么轴限制存在问题。

在此处输入图像描述

4

1 回答 1

3

从你的例子看来scatterplot,你有很多观点。将这些绘制为单独的数据将覆盖大部分数据,并且仅显示“顶部”数据。这是不好的做法,当你有这么多数据时,做一些聚合会改善视觉表现。

下面的示例显示了如何bin使用 2d 直方图平均数据。一旦您的数据采用适当的格式进行可视化显示,将结果绘制为图像或轮廓是相当简单的。

在绘图之前聚合数据还可以提高性能并防止Array Too Big或内存相关的错误。

fig, ax = plt.subplots(1, 3, figsize=(15,5), subplot_kw={'aspect': 1})

n = 100000

x = np.random.randn(n)
y = np.random.randn(n)+5
data_values = y * x

# Normal scatter, like your example
ax[0].scatter(x, y, c=data_values, marker='x', alpha=.2)
ax[0].set_xlim(-5,5)


# Get the extent to scale the other plots in a similar fashion
xrng = list(ax[0].get_xbound())
yrng = list(ax[0].get_ybound())

# number of bins used for aggregation
n_bins = 130.

# create the histograms
counts, xedge, yedge = np.histogram2d(x, y, bins=(n_bins,n_bins), range=[xrng,yrng])
sums, xedge, yedge = np.histogram2d(x, y, bins=(n_bins,n_bins), range=[xrng,yrng], weights=data_values)

# gives a warning when a bincount is zero
data_avg = sums / counts

ax[1].imshow(data_avg.T, origin='lower', interpolation='none', extent=xrng+yrng)

xbin_size = (xrng[1] - xrng[0])  / n_bins # the range divided by n_bins
ybin_size = (yrng[1] - yrng[0])  / n_bins # the range divided by n_bins

# create x,y coordinates for the histogram
# coordinates should be shifted from edge to center
xgrid, ygrid = np.meshgrid(xedge[1:] - (xbin_size / 2) , yedge[1:] - (ybin_size / 2))

ax[2].contourf(xgrid, ygrid, data_avg.T)

ax[0].set_title('Scatter')
ax[1].set_title('2D histogram with imshow')
ax[2].set_title('2D histogram with contourf')

在此处输入图像描述

于 2013-12-03T08:56:42.863 回答