0

I have dataframes which I am trying to plot them in one single plot.

However, it needs to be step-by-step by iteration. Like the one single plot should be updated at each time loop runs.

What I am trying now is

for i in range(0, len(df))
    plt.plot(df[i].values[:,0], df[i].values[:,1])
plt.show()

It seems work but it generates a graph at each iteration.

I want them all to be in one plot as it is being updated.

Thanks for your help.

Edit: Regarding the answers, you referred does not contain what I wanted. That one is just superimposing two datasets.

What I wanted was that as a new graph is superimposed, the original figure created should be updated at the next iteration, not showing them all at once after the end of the loop.

Brian Lee
  • 173
  • 3
  • 14
  • Possible duplicate of [MatPlotLib: Multiple datasets on the same scatter plot](https://stackoverflow.com/questions/4270301/matplotlib-multiple-datasets-on-the-same-scatter-plot) – Fabrizio Aug 22 '19 at 07:43

1 Answers1

0

Here's an example of a plot that gets updated automatically using matplotlib's animation feature. However, you could also call the update routine yourself, whenever necessary:

import numpy as np
import matplotlib.pyplot as plt
import pandas

import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation

df = pandas.DataFrame(data=np.linspace(0, 100, 101), columns=["colA"])


fig = plt.figure()
ax = plt.gca()

ln, = ax.plot([], [], "o", mew=2, mfc="None", ms=15, mec="r")

class dataPlot(object):
    def __init__(self):
        self.objs = ax.plot(df.loc[0,"colA"], "g*", ms=15, mew=2, mec="g", mfc="None", label="$Data$")
        fig.legend(self.objs, [l.get_label() for l in self.objs], loc="upper center", prop={"size":18}, ncol=2)

    def update(self, iFrame):
        for o in self.objs:
            o.remove()

        print("Rendering frame {:d}".format(iFrame))

        self.objs = ax.plot(df.loc[iFrame,"colA"], "g*", ms=15, mew=2, mec="g", mfc="None", label="$Data$")

        return ln,

dp = dataPlot()
ani = FuncAnimation(fig, dp.update, frames=df.index, blit=True)

plt.show()
Artur
  • 407
  • 2
  • 8