从matplotlib colorbar中挑选情节颜色?

问题描述 投票:0回答:1

我有一个循环,每次迭代都会绘制数据(在同一个图上)。我试图弄清楚如何设置它,以便根据迭代次数从颜色栏中绘制每个数据集的颜色 - 也就是说,循环的每次迭代都会绘制一条深蓝色阴影的线条(例如)比之前的线。

我的代码看起来像这样:

for k in numpy.arange(0, iterations, 500):
    h,binEdges=numpy.histogram(data1[k])
    bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
    plt.plot(bincenters,h,label=(str(k)))
plt.title(title)
plt.legend(fontsize=6)
plt.show()

(data1是一本字典。)有谁知道如何做到这一点?

python matplotlib plot colorbar
1个回答
0
投票

可以通过调用colormap获得colormap的值。色彩映射范围从0到1。 cmap = plt.get_cmap("Blues"),色彩图的中间部分是cmap(0.5)。因此,您可以使用规范化的循环索引调用colormap以从中获取颜色。

import numpy as np
import matplotlib.pyplot as plt

data = [np.random.rand(13+10*i) for i in range(10)]
data1 = dict(zip(range(10),data))

cmap=plt.get_cmap("Blues")
kn = np.arange(10)
for k in kn:
    h,binEdges=np.histogram(data1[k])
    bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
    plt.plot(bincenters,h,label=(str(k)), color=cmap(float(k)/kn.max()))

plt.legend(fontsize=6)
plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.