matplotlib动态绘制极坐标图

python绘制动态极坐标图:

方法一:利用matplotlib.animation异步绘制

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as antt

li = [element for element in range(0, 330, 30)]
fig = plt.figure(figsize=(5, 5))

ax = fig.add_subplot(111, projection='polar')  # 极坐标图绘制

def dtt(r):

    plt.cla()
    # np.linspace(np.pi / 4, np.pi / 4, 2, endpoint=False)
    theta = [np.pi/2, -np.pi/2]
    radii = [r, r]

    # 哪个角度画,长度,扇形角度,从距离圆心0的地方开始画
    bars = ax.bar(theta, radii, width=np.pi / 2, bottom=0.0)
    plt.rgrids(np.arange(0, 22, 2), angle=0)  # 绘制1-9,步长为1的圆环
    plt.thetagrids(li)

    for bar in bars:
        bar.set_facecolor(plt.cm.jet(r/9.0))
        bar.set_alpha(0.5)  # 添加颜色

    return r

def dong():
    r = 1
    dir = 0
    while True:
        if dir == 0:
            r += 1;
            if r >= 20:
                r = 20
                dir = 1
        else:
            r -= 1
            if r <= 1:
                r = 1
                dir =0
        yield r


if __name__ == "__main__":

    gif = antt.FuncAnimation(fig, dtt, dong, interval=20)
    plt.show()

 

方法二:利用matplotlib.pyplot.pause()

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as antt
import time

li = [element for element in range(0, 330, 30)]
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection='polar')  # 极坐标图绘制

def dtt(r):

    plt.cla()
    # np.linspace(np.pi / 4, np.pi / 4, 2, endpoint=False)
    theta = [np.pi/2, -np.pi/2]
    radii = [r, r]

    # 哪个角度画,长度,扇形角度,从距离圆心0的地方开始画
    bars = ax.bar(theta, radii, width=np.pi / 2, bottom=0.0)
    plt.rgrids(np.arange(0, 22, 2), angle=0)  # 绘制1-9,步长为1的圆环
    plt.thetagrids(li)

    for bar in bars:
        bar.set_facecolor(plt.cm.jet(r/9.0))
        bar.set_alpha(0.5)  # 添加颜色

    return r

if __name__ == "__main__":
    lasttime = 0
    r = 1
    dir = 0
    while True:
        if time.time() * 1000 - lasttime > 20:
            lasttime = time.time()*1000
            if dir == 0:
                r += 1
                if r >= 20:
                    r = 20
                    dir = 1
            else:
                r -= 1
                if r <= 1:
                    r = 1
                    dir = 0
            dtt(r)
           
        plt.pause(0.01)

 

方法三:

ax.relim()

ax.autoscale_view()

fig.canvas.draw()

fig.canvas.flush_events()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值