4

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!

Apostolos
  • 3,115
  • 25
  • 28
  • I do not reproduce your problems, with python3.6, and matplotlib version 2.2.2. Which versions are you using ? I have this error message: TypeError: make_image() missing 1 required positional argument: 'renderer' – Gelineau Jul 05 '18 at 21:58
  • I am using PY 2.7 and MPL 2.2.2. Now, if I add 'renderer' argument, I get no error but the output is a tuple and I can't use as an ndarray image. Maybe I am looking in the wrong direction and `make_image()` is just a misleading name. I found it n standard documentation https://matplotlib.org/1.5.0/api/image_api.html, but they don't explain what it does! MPL documentation is horrible. – Apostolos Jul 07 '18 at 10:09
  • Hi. You've been here long enough and asked a lot of questions but never accepted any single one. That's not how stackexchange communities work. [Accepting an answer](https://stackoverflow.com/help/accepted-answer) signifies that the question has been answered and reward both the asker and the answerer with some reputation. Please also spend 1 minute taking the [tour] to understand about this community – phuclv Nov 27 '19 at 13:16
  • @phuclv, I believe you are right. Although I have upvoted a lot of answers, maybe also some to my questions, I have never used the feature you are mentioning. I will do it in the certainly future, if needed. I let you know that I have posted relatively few questions and I can hardly remember having received an answer that solved my problem. So, if someone has been hurt in this story (i.e. my posting of questions) is myself, since I have received a couple of downvotes on my questions (!) and a lot of comments that didn't help at all. – Apostolos Nov 27 '19 at 19:31
  • @Apostolos you can check your new questions or some questions with many answers. You can also accept your own answer, like in this question. Here it's considered not good to say that "the question is solved" since the proper way is to accept the answer – phuclv Nov 28 '19 at 00:04

2 Answers2

6

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.

Apostolos
  • 3,115
  • 25
  • 28
4

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.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Than you. I resolved this, i.e. that a renderer is needed. The problem is that `make_image()` creates a tuple and I cannot use it to create a new ndarray image. BTW, I added your 3d argument (unsampled=False) and it makes no different in the output. – Apostolos Jul 07 '18 at 10:20
  • Yes, the problem of this question is that it is not clear what you ultimately want to achieve. I'm sure `make_image` is not too helpful, but without knowing what you aim at, one cannot help more. Once it is clear what you want, we can also discuss how the documentation can be improved. – ImportanceOfBeingErnest Jul 07 '18 at 10:22
  • Of course. In my previous comment, I mentioned that " I cannot use it to create a new ndarray image". That is, I want to create an image to use with `pyplot.imshow()` and see how the 'magnification' argument works. (Re: https://matplotlib.org/api/image_api.html -> `make_image(renderer, magnification=1.0, unsampled=False)`. They don't say anything about it. (Lazy and bad MPL documentation as always! They only refer to the source.) The only thing I found out is that a tuple is created, instead of an image -- despite of the name `make_image`! I hope the problem is clear now. :) – Apostolos Jul 09 '18 at 07:09
  • If a method is that much hidden in the documentation without a reasonable docstring, it means that it is not used very often by many people and hence you may conclude that it's probably not the best way to achieve something. One may think about making this method private actually, such that people are not tempted to use it at all. Since you call `imshow` before that it means you already have an `AxesImage`. It then makes no sense to call that method at all. Speaking about matplotlib docs, those are made by people like you and me, whatever you find bad about it you may improve yourself. – ImportanceOfBeingErnest Jul 09 '18 at 08:47
  • That being said, I still don't know what you want to achieve. Best explain it by ignoring the fact that a `make_image` method even exist. – ImportanceOfBeingErnest Jul 09 '18 at 09:01
  • You are right, and this is what matters: if there's no documentation on a function and no examples in the Web, it is most probably best to forget about it. But they don't say "deprecated" or anything like that.! Anyway, if I was interested in this f... `make_image` in the first place, it is, as I said, because of the "magnification" argument. And I have not found an other means of magnifying/zooming an image in MPL except if I also involve PIL Image ... – Apostolos Jul 10 '18 at 11:23
  • This function is used internally. You cannot deprecate it. But it's not like only because some unknown function has an argument named "magnification" it would do what you would like it to do. So I understand you want to magnify an image; magnify compared to what? Matplotlib plots images (just as all other artists) inside of axes. So if you create an axes which is twice as large, the image inside would appear twice as large. Is that what you mean? Ideally, such things would not be discussed in comments though.. You may edit your quesiton to provide the actual problem description. – ImportanceOfBeingErnest Jul 10 '18 at 11:30
  • I know, this Q&A between us has been evolved well beyond what is asked by the question, the answer to which is very simple: "Just forget about `make_image()`!" :) This closes it. – Apostolos Jul 11 '18 at 13:17
  • I have added an answer to my own question, based on your help. Thank you. – Apostolos Jul 11 '18 at 16:39