1

I've recently switched to pycharm as my main development environment for python and in general I'm quite happy with how it works. However I encountered some problems. First of all I find running script quite slow, not to mention if I show a plot using matplotlib then the interactive plot is quite slow compared to simply using IDLE.

I thought about solving this by not actually showing a plot, but rather just saving the figure. However it seems that even if I don't explictily call show() that is called in the background and then closed again. Any idea what is going on?

My code:

# plot exchange rate over the years
fig, ax = plt.subplots(1, 1, figsize=(12,9))
ax.plot(years, data, color=c[0])

# set figure properties
ax.set_title('Rate')
ax.set_xlim([min(years), max(years)])
ax.set_ylabel('Rate per x')
ax.set_xlabel('Source: '+source, horizontalalignment='right', x=1.0, fontsize=9)
ax.xaxis.set_ticks(years[::2])
fig.savefig('rate.png', dpi=144, bbox_inches='tight')
plt.close(fig)

My pycharm installation details:

PyCharm 2017.3.2 (Community Edition)
Build #PC-173.4127.16, built on December 18, 2017
JRE: 1.8.0_152-release-1024-b8 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

Any idea how I can at least switch off the opening of the interactive gui for a plot (even if I'm not calling show())? This would already help a lot. If there are ways to make it all faster that would be even beter.

3
  • Are you in interactive mode? If so, plt.ioff()? Commented Jan 12, 2018 at 14:19
  • I cannot reproduce this in Pycharm 2017.1.1. When running some code without plt.show(), no window is produces, as expected. Commented Jan 12, 2018 at 14:20
  • @DavidG that solved the problem. Please reply so that I can mark it as the correct answer. Commented Jan 12, 2018 at 14:30

1 Answer 1

2

Your matplotib could be using an interactive backend. This means that interactive mode will be enabled by default.

Therefore, as soon as you plot anything, the figure window will open and your script will continue to do whatever is next. Upon reaching the end of the script, the window will close. If the plotting of your figure is at the end of the script the window will appear to open then immediately close. You can test this by putting input() at the very end of your script and the window should stay open.

In order to fix this, you can turn interactive mode off using

plt.ioff()

In which case, calling (or not calling) plt.show() will work as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.