PyCharm Not Always Plotting In New Figures

Viewed 775

I have the latest version of PyCharm installed (2020.2.2 Pro). I have PyCharm configured to plot into the SciView.

When I run the following code 5 times it generates a new plot only twice. All other times it overwrites the previous figure.

import matplotlib.pyplot as plt
import numpy as np

plt.figure()
plt.plot(np.sin(np.linspace(0,5,100)))
plt.show()

This behavior is pretty obnoxious when one wants to compare the results of multiple plots.

My PyCharm is also configured to start a new python instance on every run of the file. I also see the same behavior if I omit the plt.figure() line.

Additionally, the new plots don't appear in any repeatable way. Like I run this code 3 times I only get one plot from the first run, but then on the 4th run I get a second plot.

If I disable plotting in SciView through Settings>Python Scientific>Show plots in tool window. I generate a new plot every time I run the code.

The plot which is replaced when I rerun the code is randomly selected as well. If I have say 4 plots and one of them gets replaced it is usually the one with the bottom thumbnail, but sometimes another random one in the list is replaced instead.

If I set Settings>Build, Execution, Deployment>Console>Use existing console for "Run with Python Console" I get a new figure each time. This is fine, except I require a fresh console for every run as I'm often modifying libraries that are imported by the code that generates the plots. If I don't get a fresh console the the imports are ignored and the existing version of the library stays in the interpreters memory. I suppose I can work around this by setting %autoreload in iPython, but it's a bit of a hack...

If you're having this issue try setting Use existing console for "Run with Python Console" and putting the following somewhere in the python file you're running

from IPython import get_ipython
ipython = get_ipython()
if ipython is not None:
    ipython.magic("%load_ext autoreload")
    ipython.magic("%autoreload")
2 Answers

Are you sure it's not plotting all the charts? Check number of charts being shown (marked in red cicrcle below). While scrolling on right it only shows two charts, but there are actually three. enter image description here

I has same problem. I was plotting two plots in PyCharm and sometimes both would plot and other times only 1. It was always the first plot of the two that was hit or miss for me.

I am using Settings>Python Scientific>Show plots in tool window and Settings>Build, Execution, Deployment>Console>Use existing console for "Run with Python Console

adding this code seemed to have fixed for me. I am on a Mac.

import matplotlib
matplotlib.use('wxAgg')
Related