matplotlib:如何将XYZ散点转换为像素图像? [英] matplotlib: how can I convert a XYZ scatter to a pixel image?
问题描述
我正在寻找一种方法来将散点图(X与Y,由Z归一化的颜色)转换为2D像素图像。也就是说如何绘制像素化图像,其中像素根据第三个变量着色?
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?
在我的例子中,我有一个星系的列表,每个星系都有天空坐标(X,Y)和距离(Z)。我想制作X对Y的像素化图像,像素颜色根据Z归一化(例如,该像素中的星系的中值Z值)。
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知道我可以做这样的事情与hexbin,但我想有方形像素,而不是六边形。 (更像imshow产生的东西)。
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).
我还在学习python,所以如果有一个简单/快速的方法来做这个
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!
推荐答案
好的 - 有两种方法可以做到这一点。一个是你有一个离散的数量的仓位(如d <10pc,10pc
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()
绘图,使您在x轴上规定RA,在y轴上规定Dec,并用距离填充绘图。 RA和Dec都是具有相应坐标的1D数组。然后你做一个二维数组与距离。确定距离的中值/平均值,然后将二维数组除以该值,以将其归一化。最后,使用等高线图(使用contourf或imshow)绘图,如:
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()
这篇关于matplotlib:如何将XYZ散点转换为像素图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!