IPython/Jupyter Input Prevents Plot Showing - input

I'm having trouble with a plot because an input prevents the plot to be displayed. I have a large code which manipulate some data and plot it, then ask some questions about the plot (with which then makes another plot). Im running the code from another notebook so, although the plot and the input are in diferent cells, the plot it isn't displayed until i make the input,here is a short example of the problem:
%matplotlib nbagg
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5,6,7,8])
input('Gimme a number: ')
supose the code above is in Notebook1, if i open a new Notebook2 and from there run Notebook1 (%run Notebook1.ipynb), the first thing that pops is the input and the only thing that appears from the plot are the controls; the plot only appears after i make the input which is not good since i need to see the plot to make the correct input. I've tried with plt.show() with no results and if i use plt.draw() the plot is displayed but in non interactive mode so i can't make zoom or move through the data, this mode only activates after i make the input.
Anyone have any idea how to solve this?

Have you tried turning on the interactive mode using plt.ion() before sending the plot command?

Calling plt.gcf().canvas.draw() after plotting and before input worked for me.

Related

DataFrame (pandas) plot opens window but there is no plot

I am unable to produce any charts in Python (matplotlib 3.5.1) with the pandas DataFrame plot() method. A window opens and the axes return value is <AxesSubplot:> as opposed to returning an object like that prints as somethig like <matplotlib.axes._subplots.AxesSubplot at 0x7f3958bcf9d0>, which is what I usually see when the plot works.
The backend is QtAgg and as far as I can tell from a poke between fora pages that report a similar problem this should be all right. This is also not a problem with needing to run matplotlib.pyplot.ion() as I see a window open, but it is black with no plot in it.
Any advice would be useful, thanks!
I have resolved the (mis)behaviour without really being sure that I have resolved the issue responsible for this misbehaviour.
As looked around for a way out, I found this discussion (How to change backends in matplotlib / Python) which made a lot of getting the back end setting right. Wondering if that would make a difference, I changed the back end. The QtAgg back end loaded by default from the matplotlibrc file and so following the instructions in the post on how to permanently change the back end, I modified the file so that the back end is now Qt5Agg. The data now plots.

How to interact with a plot/figure in Julia

I'm (ultra) new to Julia and I'm trying to figure out how to interact with a plot. To be more specific, currently I use Pyplot do display different kinds of data (in one instance I just plot multiple curves via plot() and in the other I plot the values of a 2d array as heatmaps via imshow()).
I want to be able to click on these images and handle the clicking as event in the code. Upon searching I only find old posts that say "this is currently not featured".
Does anyone have suggestions on where to look/ what to do?
Thanks in advance.

Spyder interactive plot : wait for the plot to be closed to continue

I'm working with windows using Spyder, I plot with matplotlib. My problem is that I want to do interactive plot (or sometimes plotting a lot of things) and I want spyder to wait that I close the figure to continue the code (same way as a traditional terminal would).
I tried
plt.ion(),
%mpl TkAgg
before loading matplotlib, Ipython and python console... And I can't find any solution.
If you want an example, the goal is that the "hello" prints only when I close the figure, with Spyder on windows 10.
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()
print("hello")
You need to deactivate Spyder's Matplotlib support by going to
Tools > Preferences > IPython console > Graphics
and deselecting the option called
Activate support
Then you need to change your code like this
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()
print("hello")
to set your backend (TkAgg in this case) by hand before creating your plot.
When I run the code, the desired behaviour of the plot window blocking subsequent code from being executed is already there. So I guess there are some other settings involved. For that reason I also cannot test the following, but I would suppose that you need to turn the interactive mode off before calling show.
import matplotlib.pyplot as plt
plt.ion() # turn interactive on, if it isn't already
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.draw()
# possibly do other stuff that would make the use of interactive mode useful
plt.ioff() # turn interactive off
plt.show()
print("hello")

ipython & matplotlib:chart doesn't show in ipython

Any help with the issue is appreciated. As the following photo shows, I am trying to get an horizontal bar chart and always get instead. I don't know what to do with it.
Reposting as an answer:
If you run the %matplotlib magic in IPython before creating plots, the plots will appear automatically, and won't block the prompt.
We're working with matplotlib so that this won't be necessary in matplotlib 2.0.

Matplotlib plot a series of interactve plots one plot at a time

I'm trying to plot a series of plots one plot at a time. When a plot is displayed I want to be able to interact with it (i.e. zoom, pan, etc.) and wait until I'm done before the next plot is displayed.
I saw the post matplotlib plot and then wait for raw input, but I couldn't get it to work.
I'm using Spyder and Python 3.4.
Below is the code snippet of what I'd like to do. However the plots seem to get blocked by the waiting for input.
for i in range(4):
plt.figure()
plt.plot([x for x in range(100)],'x')
plt.show(block=False)
a=input('next plot')
If you are looking for interactive plots in Python, you might want to take a look at the Charts library. It enables you to use the excellent Highcharts javascript library to make beautiful and interactive plots. Highcharts uses the HTML svg tag so all your charts are actually vector images.
Some features:
Vector plots which you can download in .png, .jpg and .svg formats so you will never run into resolution problems
Interactive charts (zoom, slide, hover over points, ...)
Usable in an IPython notebook
Explore hundreds of data structures at the same time using the asynchronous plotting capabilities.
Disclaimer: I'm the developer of the library
I believe this is resolved in the recent matplotlib v.2.0.0 release. At least it resolved this problem for me.