0

I am trying to draw scatter plot with dynamic data. I am able to draw the data points through looping; but everytime it creates new colorbar.

Here is my code:

import time
from threading import Thread
import pandas as pd
import matplotlib.pyplot as plt
import random

class RealTime:
    def __init__(self):
        self.flight_complete = True
        self.data = pd.DataFrame(data=None, columns=list('ABC'))
        self.fig=None
        self.axis = None

def on_launch(self):
    plt.ion()
    self.fig = plt.figure()
    self.axis = self.fig.add_subplot(111)

def create_data(self):
    x = round(random.uniform(-1, 1), 2)
    y = round(random.uniform(-1.65, 1.65), 2)
    z = 0.5
    temp_data = pd.DataFrame([[x, y, z]], columns=list('ABC'))
    self.data = self.data.append(temp_data, ignore_index=True)
    # Plot the data
    self.plot()

def start_fly(self):
    self.on_launch()
    fly = Thread(target=self.fly_running)
    fly.start()

def fly_running(self):
    for _ in range(10):
        print("Flying")
        # Create the data
        self.create_data()
        time.sleep(0.1)
    return

def plot(self):
    plt.gca().cla()
    self.data.plot(kind="scatter", x="A", y="B", alpha=0.4,
                        s=50, label="Real Time Position",
                        c="C", cmap=plt.get_cmap("jet"), colorbar=True, ax=self.axis)

    plt.colormaps()
    plt.title("Flight Path Map")
    self.fig.canvas.draw()
    self.fig.canvas.flush_events()


if __name__ == '__main__':
    obj = RealTime()
    obj.on_launch()
    obj.fly_running()

I have read this post : How to retrieve colorbar instance from figure in matplotlib. But I couldn't really work with that.

Do you know why it creates a new colorbar? and how to avoid it?

Best Regards

Aybars
  • 395
  • 2
  • 11

1 Answers1

0

Panda's plot is creating new colobar because you're asking it to create one (colorbar=True), and it looks like there is now way to tell the function that there is already a colorbar and that it should use that instead.

There are many ways to go around this problem.

the first one would be not not use DataFrame.plot() but instead use matplotlib directly to generate the plot. That will give you more control over the axes that are used and will let you recycle the colorbar from frame to frame. Here are some links that might be relevant:

the second option if you want to keep your code close to what it is now it to erase the whole figure at each frame, and let pandas recreate the axes it need every time. i.e.:

def plot(self):
    self.fig.clf()
    self.axis = self.fig.add_subplot(111)
    self.axis = self.data.plot(kind="scatter", x="A", y="B", alpha=0.4,
                               s=50, label="Real Time Position",
                               c="C", cmap=plt.get_cmap("jet"), colorbar=True, ax=self.axis)
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Hi thanks for the answer. Your second option also came to my mind and I have applied it. But then I see very strange behaviour. Colorbar was shifting to the left in every iteration. If you can provide me some link about the first option, I will be really glad...Thanks – Aybars Nov 16 '18 at 12:09
  • I have also tried to create my own colorbar and attach it. I attached it but then I don't know how to connect the data set to my own colorbar. This way also might be the option – Aybars Nov 16 '18 at 12:11
  • @Aybars in the case of the second option, notice that I have removed the `ax=self.axis` from the function call, as after clearing the figure `self.axis` will be undefined. Maybe that's what was causing the issue? – Diziet Asahi Nov 16 '18 at 12:15
  • yes I have noticed it after you pointer. It solved the problem of colorbar; but now it creates new figure every iteration. It seems canvas.flush.event() become unresponsive or what? – Aybars Nov 16 '18 at 12:20
  • interesting. I did not expect pandas to recreate new figures. I updated my answer with an alternate way to clear the figure, which seem to work in my tests – Diziet Asahi Nov 16 '18 at 12:31
  • yes, your modification is worked! Thanks. If I want to go in a direction of option 1, how can I set the figure properties ? How can I apply my own colorscale to data? Because I know the limit of the value limits for color scale – Aybars Nov 16 '18 at 12:47