繁体   English   中英

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

[英]Getting pixel location for matplotlib contour plot

我有一个轮廓图应用程序,我想知道轴原点的像素位置。 我已经阅读了“ 转换教程” ,但似乎无法正常工作。

这是从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

因此,您可以通过两种方式修复它:更改fig()调用的dpi以匹配savefig()的默认值:

plt.figure(dpi=100)

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

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

暂无
暂无

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

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