【问题标题】:Stack Plot with a Color Map matplotlib带有颜色图 matplotlib 的堆栈图
【发布时间】:2013-12-18 09:34:41
【问题描述】:

我想用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,并能够为我的数据生成此图表。我想知道为什么轴限制有问题。

【问题讨论】:

  • 图库中有stackplot 示例:matplotlib.org/examples/pylab_examples/stackplot_demo.html 但是在查看您的数据时,我怀疑您是否需要,堆栈图适用于不同的系列。看来您更多的是寻找等高线图:matplotlib.org/examples/pylab_examples/contour_image.html
  • @RutgerKassies 你能解释一下为什么吗?我在示例部分和其他来源中尝试了等高线图,它并没有真正反映我发布的数字。我觉得 z 值有 6 个 bin,我需要使用堆积面积图进行绘制。
  • @Dexter - 你有散点,每个点都有一个 x,y,z 值,而不是一系列线。堆叠图只是一系列线,每条线之间的间隔都被填充。您想要的(以及您链接到的图中显示的内容)是给定几个 x、y、z 观测值的插值“z”值的等高线图。您需要将分散的数据插入到常规网格中,然后使用contourf
  • 另外,附带说明一下,在调用 scatter 时,如果您所做的只是线性比例,则无需指定自定义 norm。完全保留 norm 参数将与您当前的代码具有完全相同的效果。希望对您有所帮助!
  • @Dexter - 不,只是你根本无法用stackplot 做你想做的事。 (例如,注意图中您要复制的“岛”。)如果您要尝试使用stackplot,则必须首先使用contour 来识别常量“z”的行" 用stackplot 绘制它们之前的值。 contourf 基本上只是一步完成,但它比stackplot 更灵活地绘制事物(例如,它会愉快地处理“岛屿”:又名封闭轮廓)。

标签: python-2.7 matplotlib scatter-plot stacked-area-chart


【解决方案1】:

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

【讨论】:

  • 感谢您提供详细的代码和解释。它真的很有帮助。我现在得到的数字(我已经编辑了我的原始帖子)与轴的限制有关。可能是什么原因?
猜你喜欢
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 2011-07-26
相关资源
最近更新 更多