E
E
Eva052015-11-02 13:14:00
Python
Eva05, 2015-11-02 13:14:00

How to plot in Python (Matplotlib) without blocking the console?

Yesterday I started working in the Python, Matplotlib libraries and a nuisance arose.
When plotting a chart, the console is completely blocked, it is impossible to enter anything until I close the window with the chart. After closing, the blocking disappears and you can write code again.
How can I create a chart without blocking the console?
My code:
import matplotlib.pyplot as plt
import numpy as np
t=np.arange(0.,5.,0.2)
plt.plot(t,t,'r--',t,t**2.'bs ',t,t**3,'g^'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
angru, 2015-11-02
@angru

plt.ion() + plt.draw() instead of plt.show()
There is also IPython Notebook that might be of interest. It has now been merged into a separate Jupyter project. You can try (Welcome to Python.ipynb -> Cell -> Run All) It will draw a graph for you right there, you can even paste your code and see what happens, just be careful, this is just a demo, all code is deleted later. And you can also deploy it on a local machine, if on Windows, then in general everything is simple: download anacondait includes a huge bunch of libraries (matplotlib, numpy, scipy...), run IPython (Py 3.4) Notebook, it is deployed on the local interface, on port 8888, everything, you can create your own sketches (*.ipynb), edit them directly in the browser, view graphs, save, and also fumble through Jupyter .
ps to display plots directly in the sketch, and not in a separate window, use the %matplotlib inline macro

O
Oleg Tsilyurik, 2015-11-02
@Olej

How can I create a chart without blocking the console?

Run the entire graphical part in a separate thread or process:
The subtleties of using the Python language: Part 4. A couple ... The
subtleties of using the Python language: Part 5. Multi ...
And here is another absolutely nice little thing for displaying graphs: Reinteract

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question