获取matplotlib等高线图的像素位置

时间:2015-10-02 18:59:05

标签: python matplotlib pixel contour

我有一个轮廓图应用程序,我想知道轴原点的像素位置。我已经阅读了Transformation Tutorial,但它似乎没有正常运作。

这里的代码改编自Contour Demo程序:

#!/usr/bin/env python
"""
Illustrate simple contour plotting, contours on an image with
a colorbar for the contours, and labelled contours.

See also contour_image.py.
"""
import matplotlib

matplotlib.use('Agg')

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

# Create a simple contour plot with labels using default colors.  The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')

print "Origin:\t", plt.gca().transData.transform((-3.0, -2.0))

plt.savefig("cdemo.png")

输出是: 原产地:[80。48。]

以下图片: enter image description here

但是,当我使用一个显示光标位置(以像素为单位)(GIMP)的编辑器查看时,它会将原点位置显示为(100,540)。据我所知,Matplotlib的原点是左下角,GIMP从左上角开始计数,因此使用(800,600)的图像大小进行调整,这使得我的翻译位置为(100,60)。

有什么想法吗?这是左下角用红色标记的大致位置(80,48)的图像。 enter image description here

使用matplotlib 1.4.3 谢谢!

1 个答案:

答案 0 :(得分:0)

tcaswell钉它 - 问题是图形对象和保存的图像文件之间的dpi不匹配。 figure()默认为80 dpi,而savefig()默认为100 dpi

所以你可以用两种方式解决它...... 更改figure()调用的dpi以匹配savefig()默认值:

plt.figure(dpi=100)

或者您可以更改savefig()调用的dpi以匹配figure()默认值:

plt.savefig("cdemo.png", dpi=80)
相关问题