How to remove an histogram in Matplotlib

后端 未结 2 1630
南笙
南笙 2021-01-23 10:51

I am used to work with plots that change over the time in order to show differences when a parameter is changed. Here I provide an easy example

import matplotli         


        
相关标签:
2条回答
  • 2021-01-23 11:08

    Here the right way to iteratively draw and delete histograms in matplotlib

    import matplotlib.pyplot as plt
    import numpy as np
    
    
    fig = plt.figure(figsize = (20, 10))
    ax = fig.add_subplot(111)
    ax.grid(True)
    
    
    for j in range(1, 15):
        x = np.random.randn(100)
        count, bins, bars = ax.hist(x, 40)
        plt.draw()
        plt.pause(1.5)
        t = [b.remove() for b in bars]
    
    0 讨论(0)
  • 2021-01-23 11:17

    ax.hist doesn't return what you think it does.

    The returns section of the docstring of hist (access via ax.hist? in an ipython shell) states:

    Returns
    -------
    n : array or list of arrays
        The values of the histogram bins. See **normed** and **weights**
        for a description of the possible semantics. If input **x** is an
        array, then this is an array of length **nbins**. If input is a
        sequence arrays ``[data1, data2,..]``, then this is a list of
        arrays with the values of the histograms for each of the arrays
        in the same order.
    
    bins : array
        The edges of the bins. Length nbins + 1 (nbins left edges and right
        edge of last bin).  Always a single array even when multiple data
        sets are passed in.
    
    patches : list or list of lists
        Silent list of individual patches used to create the histogram
        or list of such list if multiple input datasets.
    

    So you need to unpack your output:

    counts, bins, bars = ax.hist(x, 40)*j
    _ = [b.remove() for b in bars]
    
    0 讨论(0)
提交回复
热议问题