Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive plotting with Python via command line

I am trying to use Python and the numpy and matplotlib libraries to do some data analysis and plotting and view my plots, adjust my code accordingly etc. So I need to be able to examine the plot. However, running the script from the command line causes the figure to pop up momentarily then instantly disappear. Another answer suggested to add a raw_input("text here") line at the end of the program to force python to wait for input and hold the plots open. This does hold the plot windows open for me but the actual plots disappear and I just get an empty gray figure window while python waits for input.

I'm running MAC OS X 10.8.3 and using the terminal v2.3 and my python installation is python 2.7.3

import matplotlib.pylab as plt
import numpy as np

[ .. bunch of calculations .. ]

plt.ion()
plt.figure("Test Figure")
plt.plot(xspace[:],vals[:,50])

raw_input("press key to exit")
like image 624
Daniel Avatar asked Apr 13 '13 19:04

Daniel


People also ask

Can matplotlib be interactive?

You can make a plot in matplotlib, add interactive functionality with plugins that utilize both Python and JavaScript, and then render it with D3. mpld3 includes built-in plugins for zooming, panning, and adding tooltips (information that appears when you hover over a data point).

How do I run an interactive Python?

How to Run Python Code Interactively. A widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in python , or python3 depending on your Python installation, and then hit Enter .


1 Answers

Use plt.show at the end of your code.

like image 151
Chrismit Avatar answered Oct 25 '22 18:10

Chrismit