How to set imshow scale

2024/10/3 0:28:40

I'm fed up with matplotlib in that it's so hard to plot images in specified size.

I've two images in 32*32, 20*20 sizes. I just want to plot them in its original size, or in proportion to its original size.

After reading quite a few posts at SO, the code I'm using now is

plt.autoscale(False)
plt.subplot(1, 2, 1); 
plt.imshow(img_blob[0, 0, :, :], cmap='gray',interpolation='nearest', extent=[0, 32, 0, 32])
plt.subplot(1, 2, 2); 
plt.imshow(output_blob[0, 0, :, :], cmap='gray',interpolation='nearest', extent=[0, 20, 0, 20])
plt.show()

But the two images are still displayed in the same size.

enter image description here

I've tried figsize

plt.subplot(1, 2, 2, figsize=(2.0, 2.0)); 

But apparently there's no figsize attribute.

Is it possible to make the two subplots in different size?

Answer

There is also an alternative to imshow you could use if the image proportions are very important ... there is something called figimage which displays a completely non-resampled image to your figure. Working with it is somewhat painstaking - it doesn't do much behind the scenes to make your life easier - but it can be useful for certain circumstances. Here is something using your example

import matplotlib.pyplot as plt
import numpy as npx = np.linspace(0,1,20)
z1 = x[:,np.newaxis] * x
y = np.linspace(0,1,32)
z2 = y[:,np.newaxis] * yfig = plt.figure(figsize=(2,1))
plt.figimage(z1, 25, 25, cmap='gray') # X, Y offsets are in pixels
plt.figimage(z2, 75, 25, cmap='gray')plt.show()
https://en.xdnf.cn/q/70786.html

Related Q&A

Python distutils gcc path

Im trying to cross-compile the pycrypto package, and Im getting closer and closer however, Ive hit an issue I just cant figure out.I want distutils to use the cross-compile specific gcc- so I set the C…

TypeError: builtin_function_or_method object has no attribute __getitem__

Ive got simple python functions.def readMainTemplate(templateFile):template = open(templateFile, r)data = template.read()index1 = data.index[[] #originally I passed it into data[]index2 = data.index[]]…

Extract currency amount from string in Python

Im making a program that takes currency from a string and converts it in to other currencies. For example, if the string was the car cost me $13,250 I would need to get $ and 13250. I have this regex a…

Error: The elasticsearch backend requires the installation of requests. How do I fix it?

Im having a issue when I ran "python manage.py rebuild_index" in my app supported by haystack and elasticsearch.Python 2.7 Django version 1.6.2 Haystack 2.1.0 Elasticsearch 1.0Please see the …

numpy: applying argsort to an array

The argsort() function returns a matrix of indices that can be used to index the original array so that the result would match the sort() result.Is there a way to apply those indices? I have two array…

Jinja2 for word templating

I would like to use jinja2 for word templating like mentioned is this short article. The problem Im facing is as follows, if I put {{title}} in my word-file the resulting xml can look like this:<w:r…

API capture all paginated data? (python)

Im using the requests package to hit an API (greenhouse.io). The API is paginated so I need to loop through the pages to get all the data I want. Using something like:results = [] for i in range(1,326+…

How to convert latitude longitude to decimal in python?

Assuming I have the following:latitude = "20-55-70.010N" longitude = "32-11-50.000W"What is the easiest way to convert to decimal form? Is there some library?Would converting from…

No module named main, wkhtmltopdf issue

Im new in python, but all search results i found was useless for me.C:\Users\Aero>pip install wkhtmltopdf Collecting wkhtmltopdfUsing cached wkhtmltopdf-0.2.tar.gz Installing collected packages: wkh…

Is there a Python shortcut for variable checking and assignment?

Im finding myself typing the following a lot (developing for Django, if thats relevant):if testVariable then:myVariable = testVariable else:# something elseAlternatively, and more commonly (i.e. buildi…