matplotlib plot small image without resampling

2024/10/15 1:25:11

I'm trying to plot a small image in python using matplotlib and would like the displayed axes to have the same shape as the numpy array it was generated from, i.e. the data should not be resampled. In other words, each entry in the array should correspond to a pixel (or thereabouts) on the screen. This seems trivial, but even after trawling the internet for while, I can't seem to get it to work:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmX = np.random.rand(30,40)fig = plt.figure()
fig.add_axes(aspect="equal",extent=[0, X.shape[1], 0, X.shape[0]])
ax = fig.gca()
ax.autoscale_view(True, False, False)
ax.imshow(X, cmap = cm.gray)plt.show()
Answer

I've had the same problem myself. If the interpolation='nearest' option to imshow isn't good enough, well if your main objective is to see raw, un-scaled, non-interpolated, un-mucked about pixels in matplotlib, then you can't beat figimage IMHO. Demo:

import numpy as np
import numpy.random
import matplotlib.pyplot as plta=256*np.random.rand(64,64)f0=plt.figure()
plt.imshow(a,cmap=plt.gray())
plt.suptitle("imshow")f1=plt.figure()
plt.figimage(a,cmap=plt.gray())
plt.suptitle("figimage")plt.show()

Of course it means giving up the axes (or drawing them yourself somehow). There are some options to figimage which let you move the image around the figure so I suppose it might be possible to manoeuvre them on top of some axes created by other means.

https://en.xdnf.cn/q/69347.html

Related Q&A

Nullable ForeignKeys and deleting a referenced model instance

I have a ForeignKey which can be null in my model to model a loose coupling between the models. It looks somewhat like that:class Message(models.Model):sender = models.ForeignKey(User, null=True, blank…

UnrecognizedImageError - image insertion error - python-docx

I am trying to insert an wmf file to docx using python-docx which is producing the following traceback. Traceback (most recent call last):File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reade…

Python pool map and choosing number of processes

In setting the number of processes, Id be keen to see how many threads I can actually use on my machine - how do I find this? Is there a way to determine the number of threads available to me?

connection times out when trying to connect to mongodb atlas with python

Im trying to connect to my mongodb atlas cluster but i keep getting timed out as soon as i try to do something with my db. The db i use was created in mongoshell and also the collection i checked their…

Supervisor not working with Gunicorn + Flask

I am trying to run Gunicorn from Supervisor in an Ubuntu 12.04 system. Gunicorn runs a Flask app (simple REST web service tested with Flasks embedded server). I have installed Gunicorn by clonning GIT …

How to hash int/long using hashlib in Python?

Im developing a set of cryptographic algorithms / protocols for educational purposes. Specifically, I am currently working on OAEP encoding.OAEP involves use of cryptographic hash functions; therefore …

SQLAlchemy: Override relationship-defined order_by in a query

So, I have a model that is something like:class Foo(model):__tablename__ = "foo"id = Column(Integer, primary_key=True)data = relationship("FooData",cascade="all, delete-orphan&…

Toplevel in Tkinter: Prevent Two Windows from Opening

Say I have some simple code, like this:from Tkinter import * root = Tk() app = Toplevel(root) app.mainloop()This opens two windows: the Toplevel(root) window and the Tk() window. Is it possible to avoi…

Specify File path in tkinter File dialog

I have a file dialog to open a file, however, the file that I want to open is in a different directory than the program I wrote. The file dialog opens to the directory where I am. Is there a way to s…

Why does scipy linear interpolation run faster than nearest neighbor interpolation?

Ive written a routine that interpolates point data onto a regular grid. However, I find that scipys implementation of nearest neighbor interpolation performs almost twice as slow as the radial basis f…