我使用pyplot中的函数plot()和hist()(没有任何颜色定义)来生成以下图形:
将包括更多的数据集。 这就是为什么我要对拟合曲线和相关的直方图使用相同的颜色,以使其具有一定的可区分性。
我找不到任何与此相关的信息。
我找到了一个解决方案
plt.gca().set_color_cycle(None)
感谢在Matplotlib中重置颜色周期
下面的代码应该可以立即使用,以完成我的关于高斯拟合和直方图颜色相同的问题
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))
为了确保曲线图和直方图具有相同的颜色,我的建议是为曲线图和最佳拟合线固定颜色。
如果您在此处查看示例http://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html
然后在pyplot的python文档中http://matplotlib.org/1.2.1/api/pyplot_api.html?highlight=hist#matplotlib.pyplot.hist
matplotlib.pyplot.hist方法具有kwarg颜色,可让您选择所需的直方图颜色。在示例中,他们将facecolor ='green'设置为
然后,为了获得最佳拟合线,可以选择以相同颜色绘制它。我需要查看代码以给出更精确的指示。但是,如果我们回到此处的示例,则会设置行属性:
l = plt.plot(bins, y, 'r--', linewidth=1)
因此,如果我们希望拟合线像其他直方图一样为绿色,则可以使用:
l = plt.plot(bins, y, 'r--', linewidth=1, color = 'green')
希望这会有所帮助,如果您不发布任何代码行,则不能为您提供更多具体提示。
感谢你的付出。 同时也对缺少代码感到抱歉。 我会马上改正