简体   繁体   English

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

[英]Getting pixel location for matplotlib contour plot

I have a contour plot application that I'd like to know the pixel location of the axes origin. 我有一个轮廓图应用程序,我想知道轴原点的像素位置。 I've read through the Transformation Tutorial , but it doesn't seem to be working properly. 我已经阅读了“ 转换教程” ,但似乎无法正常工作。

Here's the code, adapted from the Contour Demo program: 这是从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")

The output is: Origin: [ 80. 48.] 输出为: 原点:[80. 48.]

And the following image: 和下面的图片: 在此处输入图片说明

However, when I look at this with an editor that shows the cursor position in pixels (GIMP) it shows the origin location as (100,540). 但是,当我使用以像素为单位显示光标位置(GIMP)的编辑器进行查看时,其原始位置显示为(100,540)。 I understand that Matplotlib's origin is lower left, and GIMP counts from upper left, so adjusting for this with the image size of (800, 600) that gives me a translated location of (100,60). 我知道Matplotlib的原点位于左下角,GIMP从左上角开始计数,因此使用(800,600)的图像大小对其进行调整,使我的翻译位置为(100,60)。

Any ideas? 有任何想法吗? Here's the image with the approximate location of (80, 48) marked in red at the lower left. 这是在左下角用红色标记的近似位置(80,48)的图像。 在此处输入图片说明

Using matplotlib 1.4.3 Thanks! 使用matplotlib 1.4.3谢谢!

tcaswell nailed it - the problem was a mismatch in dpi between the figure object, and the saved image file. tcaswell将其钉牢-问题是图形对象与保存的图像文件之间的dpi不匹配。 figure() defaults to 80 dpi, while savefig() defaults to 100 dpi Figure()默认为80 dpi,而savefig()默认为100 dpi

So you can fix it two ways... Change the dpi of the figure() call to match the savefig() default: 因此,您可以通过两种方式修复它:更改fig()调用的dpi以匹配savefig()的默认值:

plt.figure(dpi=100)

Or you can change the dpi of the savefig() call to match the figure() default: 或者,您可以更改savefig()调用的dpi以匹配fig()的默认值:

plt.savefig("cdemo.png", dpi=80)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM