I am having some trouble using Pyplot imshow
to plot an image from a numpy ndarray
called data
keeping both its aspect ratio and square-shaped pixels. The shape of the ndarray
is (112, 2182)
. Here´s the code I´m using:
import matplotlib as mpl
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
mpl.use('Agg')
import matplotlib.pyplot as plt
data = np.random.random_sample((112, 2182))
plt.figure(figsize=(25, 3.5))
plt.subplots_adjust(left=0.1, right=0.9)
ax = plt.subplot(111)
plt.axis('off')
plt.title('Title', fontweight='bold', y=1.15)
im = ax.imshow(data)
# Add same-width colorbar to the bottom of the image
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="25%", pad=0.3)
cbar = plt.colorbar(im, orientation="horizontal", cax=cax)
# Save image
plt.savefig('image.pdf', dpi=1000, format=pdf)
I thought that by setting the aspect using the shape of the ndarray
the image would show square pixels. However, the image seems to keep the aspect ratio but pixels are not squares, as you can see in zoomed image (I still can´t post images, sorry):
These outputs are the same I got when I used im = ax.imshow(data)
withouth the aspect
argument. I manually counted pixels in the y-axis of the whole plot and there are 112 of them, so it is not a matter of pixel grouping. Any ideas on how to solve this?
EDIT: the image I am plotting will also have a colorbar and a title. Besides, I need to be able to save it to a pdf file. The zoomed plot I uploaded is a screen capture of the plot zoomed in a pdf viewer.
If you use
ax.imshow(data)
you will automatically get square pixels. If instead you set the aspect to something other than1
/"equal"
, then you will not get square pixels. This is however independent of the effect observed in the question.The problem in the "zoomed image" is that the size of the data pixels is of the order of actual screen pixels. Hence the size of an image pixels might vary by as much as a screen pixel. The solution to this is to save on a figure such that one image pixels is exactly one screen pixel, or any multiple of that.
In this case this would be as simple as
You need to rescale pixels with use of imshow options: shape and extent:
The documentation says that the
aspect
parameter may be a float orSo an aspect ratio of 1.0 is what you are looking for. It is not expected that you get the output you show above given the value you calculated from the shape of the array. As @ImportanceOfBeingErnest points out
aspect='equal'
is the default, so it as easy as