【问题标题】:Python animated radar chartPython动画雷达图
【发布时间】:2017-03-20 07:17:14
【问题描述】:

我正在尝试使用 Python / Matplotlib 创建一个雷达图,其中可以使用 matplotlib 的内置动画模块“回放”测量数据。我希望数据点在遍历数据集时沿着它们各自的轴移动。我在读取数据和更新图表时遇到问题,也找不到这样的例子。

我附上了一段代码,可以让您了解我想要实现的目标:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from math import pi


class SubplotAnimation(animation.TimedAnimation):
    def __init__(self, data):

        self.data = data
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='polar')

        # Figure definition
        cat = ['A', 'B', 'C', 'D', 'E']
        values = [10, 10, 10, 10, 10]

        N = len(cat)

        x_as = [n / float(N) * 2 * pi for n in range(N)]

        # Because our chart will be circular we need to append a copy of
        # the first value of each list at the end of each list with data
        values += values[:1]
        x_as += x_as[:1]

        plt.rc('axes', linewidth=0.5, edgecolor='#888888')  # Set color of axes

        # Create polar plot
        ax = plt.subplot(111, projection='polar')

        # Set clockwise rotation. That is:
        ax.set_theta_offset(pi / 2)
        ax.set_theta_direction(-1)

        # Set position of y-labels
        ax.set_rlabel_position(0)

        # Set color and linestyle of grid
        ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)
        ax.yaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.5)

        # Set number of radial axes and remove labels
        plt.xticks(x_as[:-1], [])

        # Set yticks
        plt.yticks([20, 40, 60, 80, 100], ["20", "40", "60", "80", "100"])

        # Set axes limits
        plt.ylim(0, 100)

        # Draw ytick labels to make sure they fit properly
        for i in range(N):
            angle_rad = i / float(N) * 2 * pi

            if angle_rad == 0:
                ha, distance_ax = "center", 10
            elif 0 < angle_rad < pi:
                ha, distance_ax = "left", 1
            elif angle_rad == pi:
                ha, distance_ax = "center", 1
            else:
                ha, distance_ax = "right", 1

        ax.text(angle_rad, 100 + distance_ax, cat[i], size=10,
                horizontalalignment=ha, verticalalignment="center")

        animation.TimedAnimation.__init__(self, fig, interval=25, blit=True)

        def new_frame_seq(self):
            return iter(range(len(self.data)))

        def _draw_frame(self, framedata):
            ax.plot(ax, framedata)

testdata = [[10, 20, 30, 40, 50],
            [10, 20, 30, 40, 50],
            [40, 50, 60, 70, 80],
            [40, 50, 60, 70, 80],
            [50, 60, 70, 80, 90]]

ani = SubplotAnimation(testdata)
plt.show()

任何关于如何完成这项工作的提示将不胜感激!

【问题讨论】:

    标签: python animation matplotlib radar-chart


    【解决方案1】:

    目前尚不清楚继承 TimedAnimation 的目的是什么。这让事情变得太复杂了。

    这是一个使用FuncAnimation 的动画雷达图的简单示例。

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    import numpy as np
    
    fig = plt.figure(figsize=(4,4))
    ax = fig.add_subplot(111, projection='polar')
    ax.set_ylim(0,100)
    
    data = np.random.rand(50)*6+2
    theta = np.linspace(0,2.*np.pi, num=50)
    l,  = ax.plot([],[])
    
    def update(i):
        global data
        data += (np.random.rand(50)+np.cos(i*2.*np.pi/50.))*2 
        data[-1] = data[0]
        l.set_data(theta, data )
        return l, 
    
    ani = animation.FuncAnimation(fig, update, frames=50, interval=200, blit=True)
    plt.show()
    

    【讨论】:

    • 非常感谢您!我让它按照我最终打算的方式工作:-)
    • 谢谢!这似乎很棒!只是一个问题,我按原样粘贴您的代码。但是这个数字被锁定在第一个状态。换句话说,动画不像在 Notebook Jupyter 上那样工作。我错过了什么吗?
    • 我找到了解决方案。要在此笔记本中显示它,您必须使用代码行添加一个交互式 Javascript 小部件(动画对象的默认 HTML 表示)
    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多