0

I want to plot data and view the graph in Python while the program is running, and hence, while the data is changing ; while executing loop and before the program completes execution. I am running genetic algorithm and I would like to view statistical data of each generation as and when each generation completes and before next generation begins.Does anyone know something I could use? Thanks.

Radhika
  • 29
  • 1
  • 6

2 Answers2

0

This really depends on your environment.

If it's web-based (or even if localhost server + browser is ok), then post your data as JSON and plot with in javascript using e.g. http://www.flotcharts.org/

If you want to use matplotlib for consistency with other plotting needs, then How to update a plot in matplotlib? covers your question and is well-answered.

Finally you may choose to make your own UI, in which case SVG, SDL, GTK+, Cairo are all valid options.

Community
  • 1
  • 1
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120
  • I am currently using Python 2.7.6 with Matplotlib, pylab and numpy. All that I am able to get is a scatterplot of every generation together. What I need is to get plot for each generation separately while erasing the plot of the previous generation. Thus the plot should ideally get updated after each generation. – Radhika Oct 14 '14 at 10:47
0

To add on what has been said so far, you can make use of matplotlib.animation. There is a good demonstration using a scatter plot here. Regarding deleting handle, you can do it as simple as this

import numpy as np
import matplotlib.pyplot as plt

h = None

for i in range(3):
    data = np.random.rand(100,2) 
    if h is not None:
        h.remove()
    h, = plt.plot(data[:, 0], data[:, 1], 'oy')
    plt.pause(1)
Community
  • 1
  • 1
hashmuke
  • 3,075
  • 2
  • 18
  • 29
  • I am taking input from a text file for plotting.But I am facing an issue. I want to generate plots as and when the generations are changing. But now I am only able to plot all generations together. Is there any way to take parts of data from a text file and then plot rather than consider whole data in text file together? – Radhika Oct 15 '14 at 14:24
  • If the data doesn't create burden in memory, You can load the whole data in to a variable and plot part of this data in each loop. – hashmuke Oct 15 '14 at 19:41