Pyplot shows image without plt.show() - python

I had a look at other questions on that regard, but in all other cases people had
%matplotlib inline
which caused plots to show without being prompted. I don't have it.
My plotting code looks like this:
fig, ax = plt.subplots(figsize=(17,8))
ax = plt.hist(results_df['Diff'], bins=100, density=True, histtype='step')
plt.savefig('backtester_results/figures/rf_model_lag_{n_days}_data_{lag}_lag.png',
format='png')
It's an output from testing machine learning model I'm working on and I want to put it to run for 40 hours and there will be few hundred of these plots generated in the process. So I really want them not to just get saved in a folder and not take up memory.
Do you know how to suppress this behaviour or what could be causing it?
I'm using Python 3.6 and Spyder editor. In Spyder settings I have Graphics backend set up as 'Automatic'.

Related

Plot multiple lines but figure is already displayed(Spyder)

I am trying to plot two lines in one plot. Theoretically I know how it is done, but when I run the code
import matplotlib.pyplot as plt
plt.plot(x,y)
plt.plot(x, y*y)
plt.show()
the plots are directly showing when I plt.plot(x,y) is executed. As far as I read, the plots shouldn't appear until plt.show().
I am using Anaconda Navigator with Spyder and Python 3.8.
Does anybody know how to fix this?

Different plots with show and savefig

I am plotting histograms with quite large number of bins. I am using Spyder, Python 3.6.3. The problem I found is that the figure I am seeing in iPython console is NOT the same as the saved plots. I have seen this thread which asks a similar question, however, my problem is worse, as it's not just the fonts and sizes that vary, I am actually getting different counts!
E.g. the same script:
plt.clf()
fig, ax=plt.subplots()
fig.dpi=100
plt.hist(df['POS'], bins=nbins, range=(0,dict_l[c]))
plt.savefig('current_chr17.jpg', dpi=100)
will produce this plot as a saved figure:
and show this in iPython console (interactive mode on, I'm not even asking for plt.show):
Does anyone have any explanation as to what is going on?

Matplotlib Graph not showing up on OSX?

I am currently trying to graph data using Python3 and matplotlib. I am developing on OSx Sierra and when I run, it is not showing up. There are no errors returned. I included plt.show() in my code and it is definitely running. Any help to get this graph showing would be appreciated. Vanilla Python3, edited in Emacs, ran from both IDLE and terminal. Neither work. Thank you.
import matplotlib.pyplot as plt
plt.show()
Does not produce anything, and there are no errors. I have tried
plt.switch_backend('MacOSX') and the error persists.
Just to wrap up the comments into an answer: pyplot.show() only produces a figure, if a figure has been created. This can be done explicitly by stating
fig = plt.figure()
or implicitly by plotting something (the figure is then created in the background), e.g.
plt.plot([1,2,3])
once plt.show() is called, all currently active figures will be displayed on the screen.

Python: close the figure window and let the program go

I have a python program, say, train.py. It can be run in anaconda prompt by typing:
python train.py
In train.py, some part is written for drawing and saving figures:
import matplotlib.pyplot as plt
....... #I omit these codes
plt.savefig(....)
plt.show() #this will produce a figure window
plt.close()
In implementing the program, some figures are to be generated, which will bring the program to a temporary halt, even though plt.close() present. Then I need to manually close the pop-up figure window due to plt.show() and continue the program. How to avoid this inconvenience.
It is noted that spyder can run the program continuously, with figures displayed in its console.
plt.show() is meant to be used once in a script to show all figures present. So you can create your figures and show them all at the end
fig1 = plt.figure(1)
# do something with figure 1
fig2 = plt.figure(2)
# do something with figure 2
plt.show() # show both figures

matplotlib does not show plot()

I tried running plt.show() but no plot is shown. I had tried lots of solutions from stackoverflow including setting matplotlib backend to Qt4Agg, switching to other backends (i.e. TkAgg, Agg) and reinstalling matplotlib package but still not solving the issue. Some solutions that I had tried but not working are:
matplotlib does not show my drawings although I call pyplot.show()
Matplotlib.Pyplot does not show output; No Error
Why matplotlib does not plot?
Matplotlib does not show labels or numbers
Below is the code that I tried to run:
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel = "age"
plt.ylabel = "friends count"
plt.title = "Facebook Friends count by Age"
plt.show()
When plt.scatter(pf["age"], pf["friend_count"]) code was run, a scatter plot was shown but with no labels and title. Running plt.show() did not plot and no error was shown. Appreciate any help.
I installed Anaconda with Python 3 for Windows OS. My Mac laptop is running on Bootcamp.
The labels must use parentheses such as plt.xlabel("text"), not assigned to a string with = like you have in your example code. Make the changes in your code, save the changes, quit and reopen Spyder or whatever interpreter you are running, then run the code again.
plt.figure(1)
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel("age")
plt.ylabel("friends count")
plt.title("Facebook Friends count by Age")
plt.show()

Categories