Python keeps overwriting hist on previous plot but doesn't save it with the desired plot

前端 未结 1 621
清酒与你
清酒与你 2021-01-26 08:52

I am saving two separate figures, that each should contain 2 plots together.

The problem is that the first figure is ok, but the second one, does not gets overwritten on

相关标签:
1条回答
  • 2021-01-26 09:22

    This is happening, because you have set normed = True, which means that area under the histogram is normalized to 1. And since your bins are very wide, this means that the actual height of the histogram bars are very small (in this case so small that they are not visible)

    If you use

    n, bins, _ = plt.hist(data, bins = np.linspace(data[0], data[-1], 100), normed=True, alpha= 1)
    

    n will contain the y-value of your bins and you can confirm this yourself.
    Also have a look at the documentation for plt.hist.

    So if you set normed to False, the histogram will be visible.

    Edit: number of bins

    import numpy as np
    import matplotlib.pyplot as plt
    
    rand_data = np.random.uniform(0, 1.0, 100)
    
    fig = plt.figure()
    
    ax_1 = fig.add_subplot(211)
    ax_1.hist(rand_data, bins=10)
    
    ax_2 = fig.add_subplot(212)
    ax_2.hist(rand_data, bins=100)
    
    plt.show()
    

    will give you two plots similar (since its random) to:

    which shows how the number of bins changes the histogram. A histogram visualises the distribution of your data along one dimension, so not sure what you mean by number of inputs and bins.

    0 讨论(0)
提交回复
热议问题