简体   繁体   English

Python散点图多个Colorbar问题

[英]Python Scatter Plot Multiple Colorbar issue

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 . 我已经阅读了这篇文章: 如何从matplotlib中的图检索colorbar实例 But I couldn't really work with that. 但是我真的不能解决这个问题。

Do you know why it creates a new colorbar? 您知道为什么它会创建一个新的颜色条吗? and how to avoid it? 以及如何避免呢?

Best Regards 最好的祝福

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. Panda的图正在创建新的colobar,因为您正在要求它创建一个colobar( colorbar=True ),并且看起来现在有一种方法可以告诉函数已经有一个colorbar,而应该使用它。

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. 第一个不是不使用DataFrame.plot()而是直接使用matplotlib生成图。 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. 如果您想使代码保持现在的状态,则第二种选择是擦除每一帧的整个图形,并让pandas每次都重新创建所需的轴。 ie: 即:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM