Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: make_image() inexplicable error

In the following simple matplotlib code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0,5,0.1)
y = np.sin(1.33*x)
x1, y1 = np.meshgrid(x, y)
data = np.sin(x1) + y1**4
im = plt.imshow(data)
x = im.make_image()
...

I get the following inexplicable error in the last statement: "TypeError: make_image() takes at least 2 arguments (1 given)" And I get an even more ridiculous error if I use an argument, e.g.

x = im.make_image(magnification=2.0)

"TypeError: make_image() takes at least 2 arguments (2 given)". This is one of the most ridilulous programming errors I have ever come upon!

like image 283
Apostolos Avatar asked Jan 27 '23 19:01

Apostolos


2 Answers

I have found the missing ingredient: its a renderer. E.g.

r = plt.gcf().canvas.get_renderer()
x = im.make_image(r, magnification=2.0)

This works. Meanwhile, however, I found out with the help of a commentator here that this make_image function is not of any real use, and it is not much supported. Image maginifcation must be obtained with other means, e.g. axes.

So I consider the question solved. Thank you.

like image 179
Apostolos Avatar answered Jan 30 '23 09:01

Apostolos


See e.g. this question for why something like

TypeError: method() takes at least n arguments (n given)

is not as ridiculous as it may sound at first sight.

Here you are calling make_image without any positional argument. The signature, however, is

make_image(renderer, magnification=1.0, unsampled=False)

So you are missing the renderer argument.

In python 3.6 the error is a little more clear. It would say something like

TypeError: make_image() missing 1 required positional argument: 'renderer'

which allows to find out the problem more easily.

Apart the question stays unclear on what the desired outcome is, so that's about what one can say at this point.

like image 28
ImportanceOfBeingErnest Avatar answered Jan 30 '23 10:01

ImportanceOfBeingErnest