-2

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.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712

2 Answers2

1

to make sure the plot and the histogram have the same colour, my suggestion is that you fix the colour for the plot and for the best fit line. If you look at the example here http://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html and then at the python documentation for pyplot http://matplotlib.org/1.2.1/api/pyplot_api.html?highlight=hist#matplotlib.pyplot.hist

the matplotlib.pyplot.hist method has a kwarg color that allows you to choose the colour you want for the histogram. In the example they set facecolor='green'

Then for the best fit line, you can choose to plot it in the same colour. I would need to see the code to give more precise indications. However if we go back to the example here the line properties are set:

l = plt.plot(bins, y, 'r--', linewidth=1)

so if we wanted the fit line to be green like the rest of the histogram we would use:

l = plt.plot(bins, y, 'r--', linewidth=1, color = 'green')

Hope this helps, can't give you more specific tips if you don't post any lines of code.

Elle
  • 43
  • 2
  • 7
1

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))