[英]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.