【问题标题】:Getting pixel location for matplotlib contour plot获取 matplotlib 等高线图的像素位置
【发布时间】:2015-12-31 01:41:44
【问题描述】:

我有一个等值线图应用程序,我想知道轴原点的像素位置。我已经阅读了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 谢谢!

【问题讨论】:

  • 确保已保存图形的 dpi 与图形对象的 dpi 匹配。默认情况下,它们是 80 和 100,这可能是差异的来源。
  • 就是这样。以下修复了该问题: plt.figure(dpi=100)
  • 你能把它写下来作为你自己问题的答案吗?

标签: python matplotlib pixel contour


【解决方案1】:

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

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

plt.figure(dpi=100)

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

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

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多