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:
#!/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.]
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). 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).
Any ideas? Here's the image with the approximate location of (80, 48) marked in red at the lower left.
Using matplotlib 1.4.3 Thanks!
tcaswell nailed it - the problem was a mismatch in dpi between the figure object, and the saved image file. figure() defaults to 80 dpi, while savefig() defaults to 100 dpi
So you can fix it two ways... Change the dpi of the figure() call to match the savefig() default:
plt.figure(dpi=100)
Or you can change the dpi of the savefig() call to match the figure() default:
plt.savefig("cdemo.png", dpi=80)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.