pythonmatplotlibhistogramdata-fittinggauss

Python gaussian fit with same color as bars of histogram


I use the functions plot() and hist() from pyplot (without any color definition) to generate the following graphic:

Plot of two data sets, with each having an own gauss fit

There will be even more data sets included. That's why I want to use the same color for fit curve and the related histogram, to keep it somewhat distinguishable.

I couldn't find anything related to it.


Solution

  • I found a solution using

    plt.gca().set_color_cycle(None)
    

    Thanks to Reset color cycle in Matplotlib

    The following code should work out of the box to complete my question regarding gaussian fit with same color as bars of histogram

    import matplotlib.pyplot as plt
    import matplotlib.mlab as mlab
    import numpy as np
    
    list_of_lists = []
    
    for i in range(2):
        list = np.random.normal(0, 1, 100)
        list = list.tolist()
        list_of_lists.append(list)
    
    plt.figure()
    plt.hist(list_of_lists, bins = 10, normed=True)
    
    numb_lists = len(list_of_lists)
    
    plt.gca().set_color_cycle(None)
    
    for i in range(0, numb_lists):
        list = list_of_lists[i][:]
        mean = np.mean(list)
        variance = np.var(list)
        sigma = np.sqrt(variance)
        x = np.linspace(min(list), max(list), 100)
        plt.plot(x, mlab.normpdf(x, mean, sigma))