gpt4 book ai didi

python-2.7 - 带有颜色图 matplotlib 的堆栈图

转载 作者:行者123 更新时间:2023-12-03 23:50:05 26 4
gpt4 key购买 nike

我想用 Figure 5 of this paper 中给出的颜色图绘制堆栈图.这是相同的屏幕截图

enter image description here

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

enter image description here

我想将此散点图转换为带有颜色图的堆栈图。我有点迷失了这样做的想法。我最初的猜测是,对于每个 (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-array - 一个用于颜色条上的每个 bin。然后我可以简单地从这些派生的 z-arrays 创建一个堆积面积图。

编辑2

我关注了Rutger's code并且能够为我的数据生成这张图。我想知道为什么轴限制存在问题。

enter image description here

最佳答案

从您的示例 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')

enter image description here

关于python-2.7 - 带有颜色图 matplotlib 的堆栈图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332328/

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