Before considering how to fix the question: it's expected to do some research first. When I put "savefig() got unexpected keyword argument"
into a search engine - with the quotes - I got a single result, which was this bug report:
https://github.com/matplotlib/matplotlib/issues/19714
It seems to describe the exact problem you're talking about. It's light on details, but my guess is that the configuration of the Figure that you do causes it to remember state information about values for dpi
etc. that are mistakenly reported as having been passed as keyword arguments. Knowing this may help with minimizing the MRE, but chances are good that you still don't really have a suitable Q&A for Stack Overflow, but instead a more detailed bug report for Matplotlib. (Worth checking what happens on newer Matplotlib versions, too.)
On the other hand, if your intended question is simply "how do I suppress warnings from Matplotlib, given that I don't actually care about the issue and consider them benign?", then you really could just ask that without any code - although it's a duplicate, as was stated in the comments.
Your newly provided code does not constitute a MRE.
I can't actually even install Matplotlib 3.4.3 straightforwardly in a new venv. This out-of-date Matplotlib was apparently not prepared for backwards-incompatible changes in Numpy 2.x (i.e. it didn't upper-cap Numpy in its dependencies); Pip gets the latest Numpy (2.1.2) and Matplotlib breaks trying to use the removed np.Inf
(should be np.inf
).
After downgrading Numpy to 1.26.4 (the last pre-2 version), I can run the code without an exception - but I don't see any output. Which makes sense in that the code creates an in-memory PNG, then return
s it to nowhere. But I don't see the warnings, either (and I did try to mess with Python's -W
flag etc. to make sure they weren't being suppressed somewhere else). So as far as I can make out, this is not reproducible.
But even if I could reproduce the problem, this is not minimal. It is your responsibility to figure out what parts of the code are necessary to cause the problem, and show us only what is needed.
- For example, do you get the error if you just try the same
savefig
call on a newly created Figure, without actually adding anything to it?
- What if you use the
plt.subplots
call shown in order to initialize fig, ax
, but then don't do anything else before return fig
?
- If that doesn't reproduce the problem in your environment, what things need to be added to the process, out of the code shown, to cause it? Are the line axes relevant? The setting of tight layout? The title?
- Even if your graph creation needs to be this complex to cause the problem, and you can't figure out any non-essential steps - try to simplify the surrounding code architecture, that clearly doesn't relate to the problem. Do you need to create a class? Do you need to make the dummy
graph_map
just to look up the same dispatch method each time (as opposed to just... calling it)? Do you need a if __name__ == "__main__"
block? Can you hard-code the data
into the graph creation process? Can you simplify the data
?
To have a suitable question for Stack Overflow, you need to make sure (ideally, by trying it yourself first in a fresh venv):
Can someone else copy and paste the code from the question, in an environment set up with the specified dependencies (if you only specify the Matplotlib version, then you're responsible for checking that blindly installing Matplotlib will work; if other specific versions are needed, determine and state them too), run it, and see the problem?
Will someone else see the exact problem, directly when running the code (i.e. without having to interact any more than necessary or wait for irrelevant long calculations)?
Does this code focus on the problem by doing only the things that are needed to reproduce it?