IPython console: Display plots when using run - matplotlib

I'm using a IPython console (IPython 8.3.0) for running scripts and then use the interactive console to manually inspect variables, try something new, etc.
My problem is with plotting figures - I'm looking for a way to display figures when using e.g. %matplotlib auto and %run myscript.py in the IPython console. I expect the figures drawn in separate windows, not inline.
I'm able to get the behavior I want by using %load myscript.py and executing the code, but this is quickly cluttering the history in the console.
I want to avoid using matplotlib.pyplot.show() in my scripts - if possible.
I there a way to achieve what I want without using %load or show()?

Related

Display dataframe in a separate panel in Jupyter Lab

I would like to display my dataframe in a seperate panel/window in Jupyter Lab. Currently, I can open .csv files in a separate window (which helps write code side-by-side to the data I'm working with), but I want to do this with active dataframes as well.
Is there a way to do this native to Jupyterlab, similar to how it is done in MATLAB? If not, what is the best way to do it otherwise?
Yes. There are several ways:
Display the data frame in a cell output, then right-click on the output and select Create New View for Output
Use jupyterlab-sidecar package:
from sidecar import Sidecar
sc = Sidecar(title='My dataframe')
with sc:
display(df)
(JupyterLab 3.3+) Activate the visual debugger, go to variables list, hover over the variable and click on loop icon.
You can combine approach (1) or (2) with IPython widgets to create an interactive explorer for larger data frames, such as this pandas_explorer snippet. There are other, more complete, interactive explorers which you can embed in a sidecar or move to the new output, for example ipydatagrid.

Same code running matplotlib thru anaconda prompt and spyder give images of different clarity

I'm trying to run a deep learning code. I usually run in spyder win10. The images obtained thru matplotlib are saved using something like:
plt.savefig('./output/'+filename+'_'+str(num)+'.png',dpi=360)
Occasionally, I would run the code thru the anaconda prompt using:
python abc.py
In this case, the image would appear on-screen. I realise that if I enlarge them, I can get a much clearer image with more details, as compared to the saved image. Why is this so? I have attached some images for comparison

How to display automatically interactive plots in Atom/Juno using PyPlot and Julia

I'm using Atom/Juno as IDE for my Julia programming.
I use PyPlot for graphical representation of plots however I noticed two important aspects (I'm migrating from MATLAB)
1- figures do not appear automatically after running julia scripts but only after typing PyPlot.display_figs() in the REPL.
2- figures displayed in Atom are not interactive.
On the contrary, if I open Julia REPL from terminal and I run the same code an interactive window appears (only after typing PyPlot.display_figs()).
Can I use Atom and have interactive plots to appear automatically at the end of each script without everytime using the REPL?
You have two options:
Disable in built Atom plot Pane
Use interactive plotting backend (plotlyjs could be the best option)
Ad.1.
Go in Atom to Seetings->Packages->Julia Client->UI Options and disable the "Enable plot pane" option. Restart Atom.
Ad.2.
With the "Enable plot pane" turned on try running this code:
using Plots
plotlyjs()
Plots.plot(sin.(0:0.1:7))

PTVS plotting pandas Dataframes and Series with matplotlib in python debug interactive

Is it possible to plot pandas objects inside the PTVS interactive debugger? Is it possible to save plots to disk as jpeg's?
I think I was able to do this when I first started using PTVS (last year, its awesome by the way!) but I just tried again and I dont get any plots appearing. I cant remember if I had to do something special to get this to work and from doing some google searches I get a confusing picture of the current best practice in this regard.
I want to be able to plot diagrams from my debug interactive window, similar to what is shown on this pandas tutorial.
http://pandas.pydata.org/pandas-docs/stable/visualization.html
Is this possible?
Visual Studio Professional 2013 update 4 (latest I think)
PTVS 2.1.21008.00 (latest I think)
All help is greatly appreciated.
-Jason
[edit: more info on this http://pytools.codeplex.com/discussions/574776 ]
Unfortunately not. The regular interactive has an IPython mode that, among other things, enables inline plots. But the Debug interactive doesn't have that.
You can, of course, still load matplotlib in the Debug interactive and tell it to plot things. But because there's no integration of event loops between it and VS in that mode, the plots will basically work like modal windows - you won't be able to continue debugging or otherwise interact with VS until you close the plot window.

PyPlot show() really slow after calling PyQt4.QFileDialog.getOpenFileNames()

For an analysis application I'm trying to:
Get a list of filenames from the user using PyQt4 QFileDialog
Extract some data from each file
Plot the aggregated data using pyplot
However, getting the filenames this way causes pyplot.show() to take a really, really long time. I've distilled the problem down to the following test case:
import matplotlib.pyplot as plt
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(None)
fd = QtGui.QFileDialog(None)
filenames = fd.getOpenFileNames(caption='Select Data Files')
plt.plot([1,2,3,4,5])
plt.show()
Note that I'm not even doing anything with the filenames here--just getting them.
Selecting a single file from the dialog results in a plot time of 10 seconds. Interestingly, the time required for show() to complete goes up roughly linearly with the number of files selected. For 10 files it takes about 67 seconds for the plot to show.
This isn't a big deal for plotting data from a handful of files, but when aggregating data from Monte Carlo simulations where there are thousands of files it can take literally hours for the plot to show. Telling matplotlib to use the Qt4Agg backend results in the same behavior.
If I comment out the call to getOpenFileNames the script will complete in under a second.
I'm running the following versions of the relevant packages (should be latest):
Python 2.7
matplotlib 1.3.1
python-sip 4.13.2-1
python-qt4 4.9.1-2ubuntu1
python-sip-dev 4.13.2-1
python-qt4-dev 4.9.1-2ubuntu1
I uninstalled sip, qt4 and reinstalled them--same issue. I've seen the issue accross two separate machines--both running 32-bit Ubuntu 12.04.
Any help would be much appreciated--I've wasted an embarrassing amount of time waiting for plots to show.
Updates:
The type and name of the files selected doesn't seem to matter.
Cancelling or exiting the file dialog box results in no delay and an immediate plot.
Running the script as sudo eliminates the issue; however, the file dialog looks different when I run as sudo and may be using a different gui backend that doesn't conflict with pyplot's use of PyQt, so may be a red herring.
The program doesn't hang in getOpenFileNames, it hangs on the next call that uses PyQt. Whether that's a plot or another file dialog doesn't seem to matter--the first file dialog blocks both.
Calling app.processEvents() after running the dialog doesn't help.
Using PySide in place of PyQt4 results in the same behavior.
Using getOpenFileName instead of getOpenFileNames results in the same behavior.
Running getOpenFileNames with the DontUseNativeDialog option works (no delay)
None of the other QFileDialog options have any effect (ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, ReadOnly, HideNameFilterDetails, DontUseSheet)