使用obj设置带有极轴投影的matplotlib pcolor绘图的动画时出现问题

2024-09-28 05:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个plotting对象,该对象生成带有极轴投影的动画matplotlib pcolor打印。当前,对象可以创建一组极坐标图,也可以尝试创建这些极坐标图的动画。你知道吗

创建极轴图集(而不是动画)时,对象按计划工作。你知道吗

对象的动画部分基于这个example,它在我的系统上工作。不幸的是,在我的对象中实现的动画不起作用。有一个数字和MP4文件生成的动画,但这两个数字和太短的动画都显示一些错误的形状轴。你知道吗

有没有人对嵌入到对象中时如何在动画中捕捉这个人物系列有什么建议?你知道吗

我在windows10机器上使用python3.7,matplotlib3.03

下面给出了对象的代码和运行其实例化的代码。你知道吗

class Polar_smudge(object):
 # object for creating polar contour plots

 def __init__(self,  azimuth_grid, range_grid):
   import numpy as np

   self.azimuth_grid  = np.deg2rad(azimuth_grid)
   self.range_grid    = range_grid      
   self.fig =  None 
   self.ax =  None 
   self.images = []
 #------------------------------------------------------------------   
 def add_data(self, value_grid):
   import numpy as np

   self.value_grid = value_grid
   self.value_grid[self.value_grid<=0] = np.nan


 #------------------------------------------------------------------   
 def add_figure(self, value_grid):
    import matplotlib.pyplot as plt

    # make and set-up figure
    fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
    ax.set_theta_zero_location("N")
    ax.set_theta_direction(-1)
    ax.set_rlim([0,10])
    # make plot
    cax = ax.pcolor(self.azimuth_grid, self.range_grid, value_grid,  cmap=plt.cm.viridis_r)
    ax.grid()
    plt.show()


 #------------------------------------------------------------------   
 def start_figure(self):
    import matplotlib.pyplot as plt

    # make and set-up figure
    if self.fig is  None :
      self.fig, self.ax = plt.subplots(111, subplot_kw=dict(projection='polar'))
      self.ax[0].set_theta_zero_location("N")
      self.ax[0].set_theta_direction(-1)


 def update_figure(self, value_grid):
    import matplotlib.pyplot as plt

    # make figure and add to image list
    self.images.append((self.ax[0].pcolor(self.azimuth_grid, self.range_grid, value_grid,  cmap=plt.cm.viridis_r),))


 def end_figure(self):
    import matplotlib.animation as animation

    # animate the figure list
    im_ani = animation.ArtistAnimation(self.fig, self.images, interval=50, repeat_delay=3000,blit=True)
    im_ani.save('smudge.mp4')

#============This runs the object ====================================  
import numpy as np

azimuth_bins  = np.linspace(0, 360, 360)
range_bins     = np.linspace(0, 10, 30)

# make plotting azim range grids
range_grid, azimuth_grid = np.meshgrid(range_bins, azimuth_bins)

# this works but isnt what I want
good_smudge = Polar_smudge(azimuth_grid,range_grid)  
for ix in range(3):
    val_grid = np.random.randn(360,30)
    good_smudge.add_figure(val_grid)

# this doesnt work
bad_smudge = Polar_smudge(azimuth_grid,range_grid)  
bad_smudge.start_figure()  
for ix in range(3):
    val_grid = np.random.randn(360,30)
    bad_smudge.update_figure(val_grid)
bad_smudge.end_figure()

作为对Earnest评论的回应,我做了一些进一步的改进,似乎这个问题与嵌入到一个对象中没有联系,而且增加帧数(例如30)并不能解决这个问题。下面的代码片段提供了问题的更简洁的演示(但是缺少正确生成的figure output选项)。你知道吗

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

azimuth_bins  = np.linspace(0, 360, 60)
range_bins     = np.linspace(0, 10, 30)
images = []
# make plotting azim range grids
range_grid, azimuth_grid = np.meshgrid(range_bins, azimuth_bins)


fig,ax = plt.subplots(111, subplot_kw=dict(projection='polar'))
ax[0].set_theta_zero_location("N")
ax[0].set_theta_direction(-1)

for ix in range(30):
    val_grid = np.random.randn(60,30)
    images.append((ax[0].pcolor(azimuth_grid, range_grid, val_grid,  cmap=plt.cm.viridis_r),))


# animate the figure list
im_ani = animation.ArtistAnimation(fig, images, interval=50, repeat_delay=3000,blit=False)

im_ani.save('smudge2.mp4')

Tags: 对象importselfvalueasnprange动画

热门问题