如何获取Matplotlib生成的散点图的像素坐标? [英] How to get pixel coordinates for Matplotlib-generated scatterplot?

查看:393
本文介绍了如何获取Matplotlib生成的散点图的像素坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Matplotlib生成散点图的PNG文件.现在,对于每个散点图,除了PNG文件之外,我还 希望生成散点图中各个点的像素坐标列表.

I use Matplotlib to generate PNG files of scatterplots. Now, for each scatterplot, in addition to a PNG file, I would also like to generate a list of pixel coordinates of the various points in the scatterplot.

我用来为散点图生成PNG文件的代码基本上是这样的:

The code I use to generate the PNG files for the scatterplots is basically like this:

from matplotlib.figure import Figure
from matplotlib.pyplot import setp
from matplotlib.backends.backend_agg import FigureCanvasAgg

...

fig = Figure(figsize=(3, 3), dpi=100)
ax = fig.gca()
for (x, y), m, c in zip(points, markers, colors):
    ax.scatter(x, y, marker=m, c=c, s=SIZE, vmin=VMIN, vmax=VMAX)

# several assorted tweaks like ax.spines['top'].set_color('none'), etc.

setp(fig, 'facecolor', 'none')

# FigureCanvasAgg(fig).print_png(FILEPATH)

...(大写中的变量代表可设置的参数).

...(where the variables in UPPERCASE stand for settable parameters).

如何在生成的PNG中生成与points中的点相对应的(px, py)对像素坐标的列表?

How can I also produce a list of (px, py) pairs of the pixel coordinates in the resulting PNG corresponding to the points in points?

[

好的,这是我根据乔·肯顿(Joe Kington)的建议最终想到的.

OK, here's what I finally came up with, based on Joe Kington's suggestions.

# continued from above...

cnvs = FigureCanvasAgg(fig)
fig.set_canvas(cnvs)
_, ht = cnvs.get_width_height()
pcoords = [(int(round(t[0])), int(round(ht - t[1]))) for t in
           ax.transData.transform(points)]
fig.savefig(FILEPATH, dpi=fig.dpi)

生成的像素坐标(在pcoords中)非常接近正确的值.实际上,y坐标是完全正确的. x坐标相距1或2像素,这足以满足我的需求.

The resulting pixel coords (in pcoords) are pretty close to the correct values. In fact, the y coords are exactly right. The x coords are 1 or 2 pixels off, which is good enough for my purposes.

]

推荐答案

这样做很简单,但是要了解发生了什么,您需要阅读一下matplotlib的转换. 转换教程是一个很好的起点.

Doing this is fairly simple, but to understand what's going on, you'll need to read up a bit on matplotlib's transforms. The transformations tutorial is a good place to start.

无论如何,这是一个例子:

At any rate, here's an example:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
points, = ax.plot(range(10), 'ro')
ax.axis([-1, 10, -1, 10])

# Get the x and y data and transform it into pixel coordinates
x, y = points.get_data()
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T

# In matplotlib, 0,0 is the lower left corner, whereas it's usually the upper 
# left for most image software, so we'll flip the y-coords...
width, height = fig.canvas.get_width_height()
ypix = height - ypix

print 'Coordinates of the points in pixel coordinates...'
for xp, yp in zip(xpix, ypix):
    print '{x:0.2f}\t{y:0.2f}'.format(x=xp, y=yp)

# We have to be sure to save the figure with it's current DPI
# (savfig overrides the DPI of the figure, by default)
fig.savefig('test.png', dpi=fig.dpi)

这将产生:

Coordinates of the points in pixel coordinates...
125.09  397.09
170.18  362.18
215.27  327.27
260.36  292.36
305.45  257.45
350.55  222.55
395.64  187.64
440.73  152.73
485.82  117.82
530.91  82.91

这篇关于如何获取Matplotlib生成的散点图的像素坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆