【问题标题】:Python Matplotlib, Stem plot not working with FuncAnimationPython Matplotlib,茎图不适用于 FuncAnimation
【发布时间】:2018-11-29 19:00:51
【问题描述】:

我正在尝试从传入的数据包中绘制实时数据。我有四个子图,其中一个子图,我想将数据绘制为茎图,但是我收到以下错误:
self.stemlines.set_data(z, y) AttributeError: 'list' 对象没有属性 'set_data'

plt.plot 工作正常,但我无法让它为 plt.steam 工作。

import matplotlib

matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox
import matplotlib


class PlotEngine:

    def __init__(self, axisChanged):
        # plt.style.use('seaborn-whitegrid')
        # style.use('fivethirtyeight')
        initialMinValue = '14.4'
        initialMaxValue = '14.9'

        plt.style.use('ggplot')
        matplotlib.rc('axes', titlesize=8)  # fontsize of the axes title
        matplotlib.rc('axes', labelsize=8)
        matplotlib.rc('xtick', labelsize=8)  # fontsize of the tick labels
        matplotlib.rc('ytick', labelsize=8)  # fontsize of the tick labels   axes.titlesize
        matplotlib.rc('figure', titlesize=8)

        self.fig, self.ax = plt.subplots(2, 2)
        plt.ion()
        # plt.subplot(2, 2, 4, polar=True)
        self.fig.patch.set_facecolor('gray')
        self.axpolar = plt.subplot(2, 2, 4, projection='polar')
        self.axpolar.set_facecolor('black')
        # self.ax[1, 1] = fig.add_subplot(2, 2, 4, projection='polar')
        self.ax[0, 0].set_facecolor('black')
        self.ax[0, 1].set_facecolor('black')
        self.ax[1, 0].set_facecolor('black')
        self.ax[1, 1].set_facecolor('black')

        self.axisChanged = axisChanged
        self.slider_freq = plt.axes([0.1, 0.01, 0.3, 0.01])
        self.slider_azi = plt.axes([0.5, 0.01, 0.3, 0.01])
        self.freqAxBoxMin = plt.axes([0.55, 0.33, 0.04, 0.03])
        self.freqAxBoxMax = plt.axes([0.55, 0.28, 0.04, 0.03])
        self.freqMinValueBox = TextBox(self.freqAxBoxMin, 'Min Freq:', initial=initialMinValue)
        self.freqMaxValueBox = TextBox(self.freqAxBoxMax, 'Max Freq:', initial=initialMaxValue)
        self.aziAxBoxMin = plt.axes([0.55, 0.18, 0.04, 0.03])
        self.aziAxBoxMax = plt.axes([0.55, 0.10, 0.04, 0.03])
        self.aziMinValueBox = TextBox(self.aziAxBoxMin, 'Min Dir:', initial='-180')
        self.aziMaxValueBox = TextBox(self.aziAxBoxMax, 'Max Dir:', initial='180')
        self.zeroOne, = self.ax[0, 1].plot([], [], 'ro')
        self.oneOne, = self.axpolar.plot([], [], 'ro')
        self.markerline, self.stemlines, self.baseline, = self.ax[1, 0].stem([1], [1], bottom=-140)
        self.ax[0, 1].set_xlim([0, 60])
        self.ax[0, 1].set_ylim([-140, -40])
        self.axpolar.set_yticks(range(-90, -30, 10))  # Define the yticks
        # self.axpolar.set_yticklabels(map(str, range(-90, -30, -10)))  # Change the labels
        self.ax[1, 0].set_xlim([14, 14.8])
        self.ax[1, 0].set_ylim([-140, -40])

    # self.background = fig.canvas.copy_from_bbox(self.ax.bbox)

    def animateZeroOne(self, i, azimuth, rss, freqGhz):
        x = azimuth
        y = rss
        z = freqGhz

        self.zeroOne.set_data(x, y)
        self.oneOne.set_data(x, y)
        self.stemlines.set_data(z, y)
        self.markerline.set_data(z, y)

        return self.zeroOne, self.oneOne, self.stemlines, self.markerline

【问题讨论】:

    标签: python animation matplotlib plot


    【解决方案1】:

    根据StemContainerstemlines 是一个列表(Line2D),所以它没有属性set_data,如markerlinebaselineLine2D 类型)。也许您想在animateZeroOne 函数中将该函数应用于列表的每个成员?:

        def animateZeroOne(self, i, azimuth, rss, freqGhz):
            x = azimuth
            y = rss
            z = freqGhz
    
            self.zeroOne.set_data(x, y)
            self.oneOne.set_data(x, y)
            [x.set_data(z, y) for x in self.stemlines]
            self.markerline.set_data(z, y)
    
            return self.zeroOne, self.oneOne, self.stemlines, self.markerline
    

    【讨论】:

    • 它确实将标记连接在一起但是我想要从 xaxis 到标记的垂直线。
    • 你能说明你是如何使用你的PlotEngine类的吗?
    • plotAmpliFreq = PlotEngine(fig, axisChanged=False) plotAmpliFreq.PlotAnimateLine(frequencyGhz, azimuth, rss)
    • AttributeError: 'PlotEngine' object has no attribute 'PlotAnimateLine'
    猜你喜欢
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多