我想用Figure 5 of this paper中给出的颜色图绘制一个堆栈图。这是相同的截图
目前,我能够绘制出类似性质的散点图。
我想将此散点图转换为带有色彩映射的堆栈图。我有点迷失于这样做的想法。我最初的猜测是,对于每个(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's code并能够为我的数据生成此图表。我想知道为什么轴限制存在问题。
答案 0 :(得分:3)
从您的示例scatterplot
看来,您有很多要点。将这些数据绘制为单个数据将覆盖您的大部分数据,并仅显示“顶部”数据。这是不好的做法,当你有这么多数据做一些聚合时,会改善视觉表现。
以下示例显示了如何使用二维直方图bin
和平均数据。一旦您的数据采用适当的视觉显示格式,将结果绘制为图像或轮廓就相当简单。
在绘图之前聚合数据还可以提高性能并防止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')