Matplotlib plotting undefined plot out of nowhere
First of all, I apologize if this is not the place to post these kind of questions.
The issue is that matplotlib is apparently plotting a graph (I can recognize it from an old plot, so it's not some completely random graph) whenever I execute a script. Let me explain further.
The file is a test file, module_test.py and I have been trying to recognize where this problem may come from. Now I have written
import matplotlib.pyplot as plt
plt.show()
literally at the top of the script and when I run the test via python -m package.tests.module_test, that "random" graph pops up. It also pops up with any other test script, but doesn't show up when using python in the command console. The tests run fine otherwise.
Does anybody have any idea why this happens? I have tried deleting __pycache__ from all folders related to the issue, but of no help.
Here is the picture. It's a plot from a lab report I did months ago.

python matplotlib
add a comment |
First of all, I apologize if this is not the place to post these kind of questions.
The issue is that matplotlib is apparently plotting a graph (I can recognize it from an old plot, so it's not some completely random graph) whenever I execute a script. Let me explain further.
The file is a test file, module_test.py and I have been trying to recognize where this problem may come from. Now I have written
import matplotlib.pyplot as plt
plt.show()
literally at the top of the script and when I run the test via python -m package.tests.module_test, that "random" graph pops up. It also pops up with any other test script, but doesn't show up when using python in the command console. The tests run fine otherwise.
Does anybody have any idea why this happens? I have tried deleting __pycache__ from all folders related to the issue, but of no help.
Here is the picture. It's a plot from a lab report I did months ago.

python matplotlib
1
Is therematplotlib-using code somewhere else in thepackagein question?
– Davis Herring
Jan 2 at 0:32
@DavisHerring wow, yes. And I found the module that's doing that - it had a plot that wasn't closed, and it was getting imported by the__all__inside the__init__from the subpackage containing it. I'm surprised such bizarre things could happen. But it's fixed now. Thank you. Should I delete the question or let someone answer in case someone else comes against this problem as well?
– mariogarcc
Jan 2 at 0:40
I think it’s a common enough problem (becausepyplotis so broken).
– Davis Herring
Jan 2 at 0:46
@DavisHerring Ok, I'll leave it up then. I couldn't find any solutions on the net after 1 hour of exhaustive searching. If nobody answers I'll post an answer myself tomorrow to close the question.
– mariogarcc
Jan 2 at 0:53
add a comment |
First of all, I apologize if this is not the place to post these kind of questions.
The issue is that matplotlib is apparently plotting a graph (I can recognize it from an old plot, so it's not some completely random graph) whenever I execute a script. Let me explain further.
The file is a test file, module_test.py and I have been trying to recognize where this problem may come from. Now I have written
import matplotlib.pyplot as plt
plt.show()
literally at the top of the script and when I run the test via python -m package.tests.module_test, that "random" graph pops up. It also pops up with any other test script, but doesn't show up when using python in the command console. The tests run fine otherwise.
Does anybody have any idea why this happens? I have tried deleting __pycache__ from all folders related to the issue, but of no help.
Here is the picture. It's a plot from a lab report I did months ago.

python matplotlib
First of all, I apologize if this is not the place to post these kind of questions.
The issue is that matplotlib is apparently plotting a graph (I can recognize it from an old plot, so it's not some completely random graph) whenever I execute a script. Let me explain further.
The file is a test file, module_test.py and I have been trying to recognize where this problem may come from. Now I have written
import matplotlib.pyplot as plt
plt.show()
literally at the top of the script and when I run the test via python -m package.tests.module_test, that "random" graph pops up. It also pops up with any other test script, but doesn't show up when using python in the command console. The tests run fine otherwise.
Does anybody have any idea why this happens? I have tried deleting __pycache__ from all folders related to the issue, but of no help.
Here is the picture. It's a plot from a lab report I did months ago.

python matplotlib
python matplotlib
asked Jan 2 at 0:10
mariogarccmariogarcc
128110
128110
1
Is therematplotlib-using code somewhere else in thepackagein question?
– Davis Herring
Jan 2 at 0:32
@DavisHerring wow, yes. And I found the module that's doing that - it had a plot that wasn't closed, and it was getting imported by the__all__inside the__init__from the subpackage containing it. I'm surprised such bizarre things could happen. But it's fixed now. Thank you. Should I delete the question or let someone answer in case someone else comes against this problem as well?
– mariogarcc
Jan 2 at 0:40
I think it’s a common enough problem (becausepyplotis so broken).
– Davis Herring
Jan 2 at 0:46
@DavisHerring Ok, I'll leave it up then. I couldn't find any solutions on the net after 1 hour of exhaustive searching. If nobody answers I'll post an answer myself tomorrow to close the question.
– mariogarcc
Jan 2 at 0:53
add a comment |
1
Is therematplotlib-using code somewhere else in thepackagein question?
– Davis Herring
Jan 2 at 0:32
@DavisHerring wow, yes. And I found the module that's doing that - it had a plot that wasn't closed, and it was getting imported by the__all__inside the__init__from the subpackage containing it. I'm surprised such bizarre things could happen. But it's fixed now. Thank you. Should I delete the question or let someone answer in case someone else comes against this problem as well?
– mariogarcc
Jan 2 at 0:40
I think it’s a common enough problem (becausepyplotis so broken).
– Davis Herring
Jan 2 at 0:46
@DavisHerring Ok, I'll leave it up then. I couldn't find any solutions on the net after 1 hour of exhaustive searching. If nobody answers I'll post an answer myself tomorrow to close the question.
– mariogarcc
Jan 2 at 0:53
1
1
Is there
matplotlib-using code somewhere else in the package in question?– Davis Herring
Jan 2 at 0:32
Is there
matplotlib-using code somewhere else in the package in question?– Davis Herring
Jan 2 at 0:32
@DavisHerring wow, yes. And I found the module that's doing that - it had a plot that wasn't closed, and it was getting imported by the
__all__ inside the __init__ from the subpackage containing it. I'm surprised such bizarre things could happen. But it's fixed now. Thank you. Should I delete the question or let someone answer in case someone else comes against this problem as well?– mariogarcc
Jan 2 at 0:40
@DavisHerring wow, yes. And I found the module that's doing that - it had a plot that wasn't closed, and it was getting imported by the
__all__ inside the __init__ from the subpackage containing it. I'm surprised such bizarre things could happen. But it's fixed now. Thank you. Should I delete the question or let someone answer in case someone else comes against this problem as well?– mariogarcc
Jan 2 at 0:40
I think it’s a common enough problem (because
pyplot is so broken).– Davis Herring
Jan 2 at 0:46
I think it’s a common enough problem (because
pyplot is so broken).– Davis Herring
Jan 2 at 0:46
@DavisHerring Ok, I'll leave it up then. I couldn't find any solutions on the net after 1 hour of exhaustive searching. If nobody answers I'll post an answer myself tomorrow to close the question.
– mariogarcc
Jan 2 at 0:53
@DavisHerring Ok, I'll leave it up then. I couldn't find any solutions on the net after 1 hour of exhaustive searching. If nobody answers I'll post an answer myself tomorrow to close the question.
– mariogarcc
Jan 2 at 0:53
add a comment |
1 Answer
1
active
oldest
votes
By way of its emulation of the MATLAB GUI, matplotlib (especially but not only pyplot) is stateful. This is a polite way of saying that it has global variables and all the debugging pain that goes with them.
In particular, every plot created is retained indefinitely so that it can be referenced by its “figure number”. It can also be the “current figure” for pyplot.
Importing a module from a package implicitly imports the containing package(s) first. This is one of the reasons that merely importing a module should not have any side effects (but merely define classes, functions, and constants). The combination of invisible side effects and action at a distance produces hilarity like that observed here.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999951%2fmatplotlib-plotting-undefined-plot-out-of-nowhere%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
By way of its emulation of the MATLAB GUI, matplotlib (especially but not only pyplot) is stateful. This is a polite way of saying that it has global variables and all the debugging pain that goes with them.
In particular, every plot created is retained indefinitely so that it can be referenced by its “figure number”. It can also be the “current figure” for pyplot.
Importing a module from a package implicitly imports the containing package(s) first. This is one of the reasons that merely importing a module should not have any side effects (but merely define classes, functions, and constants). The combination of invisible side effects and action at a distance produces hilarity like that observed here.
add a comment |
By way of its emulation of the MATLAB GUI, matplotlib (especially but not only pyplot) is stateful. This is a polite way of saying that it has global variables and all the debugging pain that goes with them.
In particular, every plot created is retained indefinitely so that it can be referenced by its “figure number”. It can also be the “current figure” for pyplot.
Importing a module from a package implicitly imports the containing package(s) first. This is one of the reasons that merely importing a module should not have any side effects (but merely define classes, functions, and constants). The combination of invisible side effects and action at a distance produces hilarity like that observed here.
add a comment |
By way of its emulation of the MATLAB GUI, matplotlib (especially but not only pyplot) is stateful. This is a polite way of saying that it has global variables and all the debugging pain that goes with them.
In particular, every plot created is retained indefinitely so that it can be referenced by its “figure number”. It can also be the “current figure” for pyplot.
Importing a module from a package implicitly imports the containing package(s) first. This is one of the reasons that merely importing a module should not have any side effects (but merely define classes, functions, and constants). The combination of invisible side effects and action at a distance produces hilarity like that observed here.
By way of its emulation of the MATLAB GUI, matplotlib (especially but not only pyplot) is stateful. This is a polite way of saying that it has global variables and all the debugging pain that goes with them.
In particular, every plot created is retained indefinitely so that it can be referenced by its “figure number”. It can also be the “current figure” for pyplot.
Importing a module from a package implicitly imports the containing package(s) first. This is one of the reasons that merely importing a module should not have any side effects (but merely define classes, functions, and constants). The combination of invisible side effects and action at a distance produces hilarity like that observed here.
edited Jan 2 at 2:36
answered Jan 2 at 2:29
Davis HerringDavis Herring
8,6541736
8,6541736
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999951%2fmatplotlib-plotting-undefined-plot-out-of-nowhere%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

1
Is there
matplotlib-using code somewhere else in thepackagein question?– Davis Herring
Jan 2 at 0:32
@DavisHerring wow, yes. And I found the module that's doing that - it had a plot that wasn't closed, and it was getting imported by the
__all__inside the__init__from the subpackage containing it. I'm surprised such bizarre things could happen. But it's fixed now. Thank you. Should I delete the question or let someone answer in case someone else comes against this problem as well?– mariogarcc
Jan 2 at 0:40
I think it’s a common enough problem (because
pyplotis so broken).– Davis Herring
Jan 2 at 0:46
@DavisHerring Ok, I'll leave it up then. I couldn't find any solutions on the net after 1 hour of exhaustive searching. If nobody answers I'll post an answer myself tomorrow to close the question.
– mariogarcc
Jan 2 at 0:53