获取 matplotlib 等高线图的像素位置
Getting pixel location for matplotlib contour plot
我有一个等高线图应用程序,我想知道轴原点的像素位置。我已经通读了 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.]
以及下图:
但是,当我使用以像素为单位显示光标位置的编辑器 (GIMP) 进行查看时,它显示原点位置为 (100,540)。我知道 Matplotlib 的原点在左下角,而 GIMP 从左上角开始计数,因此使用 (800, 600) 的图像大小进行调整,得到 (100,60) 的翻译位置。
有什么想法吗?这是左下角用红色标记的 (80, 48) 的大致位置的图像。
使用 matplotlib 1.4.3
谢谢!
tcaswell 搞定了 - 问题是图形对象和保存的图像文件之间的 dpi 不匹配。 figure() 默认为 80 dpi,而 savefig() 默认为 100 dpi
所以你可以通过两种方式修复它...
更改 figure() 调用的 dpi 以匹配 savefig() 默认值:
plt.figure(dpi=100)
或者您可以更改 savefig() 调用的 dpi 以匹配 figure() 默认值:
plt.savefig("cdemo.png", dpi=80)
我有一个等高线图应用程序,我想知道轴原点的像素位置。我已经通读了 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.]
以及下图:
但是,当我使用以像素为单位显示光标位置的编辑器 (GIMP) 进行查看时,它显示原点位置为 (100,540)。我知道 Matplotlib 的原点在左下角,而 GIMP 从左上角开始计数,因此使用 (800, 600) 的图像大小进行调整,得到 (100,60) 的翻译位置。
有什么想法吗?这是左下角用红色标记的 (80, 48) 的大致位置的图像。
使用 matplotlib 1.4.3 谢谢!
tcaswell 搞定了 - 问题是图形对象和保存的图像文件之间的 dpi 不匹配。 figure() 默认为 80 dpi,而 savefig() 默认为 100 dpi
所以你可以通过两种方式修复它... 更改 figure() 调用的 dpi 以匹配 savefig() 默认值:
plt.figure(dpi=100)
或者您可以更改 savefig() 调用的 dpi 以匹配 figure() 默认值:
plt.savefig("cdemo.png", dpi=80)