Let's say I have a picture taken with a sensor where the pixel size is 1mm.
I would like to show the image with imshow
: the main axes should show the pixel while the secondary axes should show the mm.
frassino.png
is the following picture
from matplotlib import pyplot as plt
import cv2
import numpy as np
a = cv2.imread('frassino.png')
fig,ax = plt.subplots(1)
ax.imshow(a,aspect='equal')
ax.set_xlabel('pixel')
ax.set_ylabel('pixel')
ax.figure.savefig('1.png')
1.png
is the following picture, all is fine (I need the pixel to be square and so I add the argument aspect='equal'
.
Now I add a secondary y axis:
v2 = ax.twinx()
v2.set_yticks(np.linspace(0,48,12))
v2.set_xlabel('mm')
ax.figure.savefig('2.png')
2.png
is the following picture and I have two problems: first, the image is cropped and the upper part of the tree, like the foreground grass, is not visible; second, the mm
label is truncated.
Now I add the secondary x axis:
h2 = ax.twiny()
h2.set_xticks(np.linspace(0,64,8))
h2.set_xlabel('mm')
ax.figure.savefig('3.png')
The following picture is 3.png
, the mm
label is there but the image is still cropped.
How can the crop be avoided?
How can the y mm
label be fixed?