Graphics and Visualization Python dynamic bar matplotlib

bar reference links: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.bar.html

 

The first approach

One way is redrawn each time, including clearing figure

Animate DEF (Fi): 
        Bars = [] 
        IF len (Frames)> Fi: 
            # axs.text (0.1,0.90, time_template% (the time.time () - START_TIME), Transform = axs.transAxes) # so this 
            time_text. set_text (time_template% (0.1 * fi )) # this must not axs.cla () job 
            # axs.cla () 
            axs.set_title ( 'bubble_sort_visualization') 
            axs.set_xticks ([]) 
            axs.set_yticks ([]) 
            Bars = axs.bar (list (range (Data.data_count )), # number 
                         [d.value for d in frames [fi ]], # data 
                         1, # width 
                         color = [d.color for d in frames [fi] ] # color  
                         ) .get_children ()
        return bars
    anim=animation.FuncAnimation(fig,animate,frames=len(frames), interval=frame_interval,repeat=False)

 

This efficiency is low, and there are some undesirable drawbacks, such needs to be reset each time xticks, if the figure add other things, these things are a clear and the need to re-add, such as text, or labale .

 

The second approach

Reference Links: https://stackoverflow.com/questions/16249466/dynamically-updating-a-bar-plot-in-matplotlib

The links in the content and almost above: https://stackoverflow.com/questions/34372021/python-matplotlib-animate-bar-and-plot-in-one-picture/34372367#34372367

May be updated as the high bar as usual update data objects

matplotlib.pyplot AS PLT Import 
Import numpy AS NP 
from matplotlib Import Animation 


Fig plt.figure = (. 1, figsize = (4,3-)) 
AX = fig.add_subplot (111) 
ax.set_title ( 'bar_animate_test') 
# ax.set_xticks ([]) Note this is to see changes, no changes in either, right, to see the changes to the annotation bar 
# ax.set_yticks ([]) 
ax.set_xlabel ( 'xlable') 
N =. 5 
= 50 Frames 
X = np.arange (. 1,. 1 + N) 

Collection = [] 
collection.append ([I I in for X]) 
for I in Range (Frames): 
    collection.append ([CI for CI +. 1 in Collection [I]]) 
Print (Collection) 
, Xstd = [0,1,2,3,4] 
Bars = ax.bar (X, Collection [0], 0.30) 
DEF Animate (Fi): 
    # Collection = [I + 1 for i in x]
   ax.set_ylim (0, max (collection [ fi]) + 3) # 3 to the problem, this is added 
    for RECT, Yi in ZIP (Bars, Collection [Fi]): 
        rect.set_height (Yi) 
    # bars.set_height ( Collection) 
    return Bars 
Anim = animation.FuncAnimation (Fig, Animate, Frames Frames =, 10 = interval The, REPEAT = False) 
plt.show ()

  

  

problem

  *)TypeError: 'numpy.int32' object is not iterable

np.arange = X (. 1,. 1 + N) 
Collection = [I I in for X] # = Collection [I I in list for (X)] is the reason that the error # dtype will here into a list ( the X-) for i in the Range (Frames): collection.append ([CI + 1 for CI in collection [i]]) # of the problem is because at this time the collection was a one array, so this collection [i] is a x where a number, not a list of the reasons, I actually thought of dtype, but also changed xstd = [0,1,2,3,4]

  should be

collection=[]
collection.append([i for i in x])#成为二维数组
for i in range(frames):
    collection.append([ci+1 for ci in collection[i]])

  Then appeared the following questions:

  *)TypeError: only size-1 arrays can be converted to Python scalars

Traceback (most recent call last):
  File "forTest.py", line 22, in <module>
    bars=ax.bar(x,collection,0.30)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\__init__.py", line 1589, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar
    label='_nolegend_',
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 707, in __init__
    Patch.__init__(self, **kwargs)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 89, in __init__
    self.set_linewidth(linewidth)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 368, in set_linewidth
    self._linewidth = float(w)
TypeError: only size-1 arrays can be converted to Python scalars

  Reference Links: https://www.cnblogs.com/Summerio/p/9723099.html

  Parameter error should be passed, carefully thought for a moment, in a given line of code, collection turned out to be wrong, because the original is one-dimensional array, now turned into a two-dimensional instead

bars=ax.bar(x,collection[0],0.30)

  All right

   *) Problems, in the above code, the running time will not will not change the size of the canvas, and the bar graph will overflow, the addition of the animate () in

def animate(fi):
    # collection=[i+1 for i in x]
    ax.set_ylim(0,max(collection[fi])+3)#添加了这个
    for rect ,yi in zip(bars,collection[fi]):
        rect.set_height(yi)

    # bars.set_height(collection)
    return bars

  

  

Other property

  *) Bar graph is how to control the interval:

  By controlling the width

width = 1, # no interval, each bar will immediately

  *)errorbar:

  A horizontal line is added, can be adjusted xerr direction and yerr

xstd = [0,1,2,3,4] 
bars = ax.bar (x, collection, 0.30 xerr = xstd)

  

 

Guess you like

Origin www.cnblogs.com/Gaoqiking/p/11261336.html