matplotlib:如何将 XYZ 散点图转换为像素图像?

发布于 2024-12-13 04:30:55 字数 331 浏览 3 评论 0原文

我正在寻找某种方法将散点图(X 与 Y,由 Z 标准化的颜色)转换为 2D“像素”图像。即,如何绘制像素化图像,其中像素根据第三个变量着色?

就我而言,我有一个星系列表,每个星系都有天空坐标(X,Y)和距离(Z)。我想制作 X 与 Y 的像素化图像,像素颜色根据 Z 进行归一化(例如该像素中星系的中值 Z 值)。

我知道我可以用 hexbin 做这样的事情,但我想要方形像素,而不是六边形。 (更像是 imshow 产生的东西)。

我仍在学习Python,所以如果有一种简单/快速的方法来做到这一点(或者关于如何以复杂的方式做到这一点的明确说明!)那就太好了。

任何帮助将不胜感激!

I'm looking for some way in to convert a scatter plot (X vs Y, color normalized by Z) into a 2D "pixel" image. I.e. how can I plot a pixelized image where the pixels are colored according to a third variable?

In my case, I have a list of galaxies, each a with sky coordinate (X,Y) and a distance (Z). I want to make a pixelized image of X vs Y, with the pixels color normalized according to Z (e.g. the median Z value for the galaxies in that pixel).

I know I could do something like this with hexbin, but I would like to have square pixels, not hexagons. (Something more like what imshow produces).

I'm still learning python, so if there is a simple/quick way to do this (or clear instructions on how to do it the complicated way!) that'd be great.

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-12-20 04:30:55

好的 - 有两种方法可以做到这一点。一种方法是为距离设置离散数量的 bin(例如 d < 10pc、10pc < d < 20pc、d> 20pc)。这相对容易,您需要做的只是几个循环 - 这是一个带有 3 的示例:

raclose = []
ramid = []
rafar = []
decdlose = []
decmid = []
decfar = []

for ii in range(len(dist)):
  if dist[ii] < 10.:
    raclose.append(ra[ii])
    decclose.append(dec[ii])
  elif dist[ii] > 20.:
    rafar.append(ra[ii])
    decfar.append(dec[ii])
  else:
    ramid.append(ra[ii])
    decmid.append(dec[ii])

plt.clf
ax1 = scatter(raclose, decclose, marker='o', s=20, color="darkgreen", alpha=0.6)
ax2 = scatter(ramid, decmid, marker='o', s=20, color="goldenrod", alpha=0.6)
ax3 = scatter(rafar, decfar, marker='o', s=20, color="firebrick", alpha=0.6)
line1 = Line2D(range(10), range(10), marker='o', color="darkgreen")
line2 = Line2D(range(10), range(10), marker='o',color="goldenrod")
line3 = Line2D(range(10), range(10), marker='o',color="firebrick")
plt.legend((line1,line2,line3),('d < 10pc','20pc > d > 10pc', 'd > 20pc'),numpoints=1, loc=3)
show()

或者您可以做一个等高线图,这样您在 x 轴上规定 RA,在 y 轴上规定 Dec 并填写与距离的情节。 RA 和 Dec 都是具有各自坐标的一维数组。然后用距离创建一个二维数组。确定距离的中值/平均值是多少,然后将二维数组除以该值以对其进行标准化。最后,使用等高线图(使用contourf或imshow)进行绘图,例如:

import matplotlib.pyplot as plt
from matplotlib import cm
ax = pylab.contourf(RA,Dec,dists, levels=[1, 5, 10, 15], cmap=plt.cm.spectral)
cbar=pylab.colorbar()

Okay - there are two ways that you can do this. One would be for you to have a discreet number of bins for the distances (like d < 10pc, 10pc < d < 20pc, d> 20pc). This is relatively easy, all you need to do are a few loops - here is an example with 3:

raclose = []
ramid = []
rafar = []
decdlose = []
decmid = []
decfar = []

for ii in range(len(dist)):
  if dist[ii] < 10.:
    raclose.append(ra[ii])
    decclose.append(dec[ii])
  elif dist[ii] > 20.:
    rafar.append(ra[ii])
    decfar.append(dec[ii])
  else:
    ramid.append(ra[ii])
    decmid.append(dec[ii])

plt.clf
ax1 = scatter(raclose, decclose, marker='o', s=20, color="darkgreen", alpha=0.6)
ax2 = scatter(ramid, decmid, marker='o', s=20, color="goldenrod", alpha=0.6)
ax3 = scatter(rafar, decfar, marker='o', s=20, color="firebrick", alpha=0.6)
line1 = Line2D(range(10), range(10), marker='o', color="darkgreen")
line2 = Line2D(range(10), range(10), marker='o',color="goldenrod")
line3 = Line2D(range(10), range(10), marker='o',color="firebrick")
plt.legend((line1,line2,line3),('d < 10pc','20pc > d > 10pc', 'd > 20pc'),numpoints=1, loc=3)
show()

Or you can do a contour plot, such that you stipulate RA on the x-axis and Dec on the y-axis and fill in the plot with the distances. Both RA and Dec are 1D arrays with the respective coordinates. Then you make a 2D array with the distance. Determine what the median/mean value of the distances are and then divide the 2D array by that value to normalize it. Finally, plot using a contour plot (using contourf or imshow), like:

import matplotlib.pyplot as plt
from matplotlib import cm
ax = pylab.contourf(RA,Dec,dists, levels=[1, 5, 10, 15], cmap=plt.cm.spectral)
cbar=pylab.colorbar()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文