着色matplotlib散点图由第三个变量和日志颜色条

时间:2016-07-01 14:49:47

标签: python multidimensional-array matplotlib colorbar

我正在尝试制作两个数组/列表的散点图,其中一个是x坐标,另一个是y。我没有遇到任何麻烦。但是,我需要根据我在二维数组中的数据,根据特定时间点的值对这些点进行颜色编码。此外,这个2d数据阵列具有非常大的范围,所以我想以对数方式对点进行着色(我不确定这是否只是改变颜色条标签或者是否存在更根本的区别。)

到目前为止,这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(1)

time = #I'd like to specify time here. 

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

multi_array = [[1, 1, 10, 100, 1000], [10000, 1000, 100, 10, 1], [300, 400, 5000, 12, 47]]

for counter in np.arange(0, 5):
    t = multi_array[time, counter] #I tried this, and it did not work. 
    s = plt.scatter(x[counter], y[counter], c = t, marker = 's')

plt.show()

我按照我在其他地方看到的建议通过第三个变量着色,即将颜色设置为等于该变量,但是当我用我的数据集尝试时,我只将所有点作为一种颜色,并且然后,当我尝试使用此模型时,它会给我以下错误:

TypeError: list indices must be integers, not tuple

有人可以帮助我按照我需要的方式为我的观点着色吗?

2 个答案:

答案 0 :(得分:2)

如果我理解这个问题(我完全不确定),这里就是答案:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(1)

time = 2 #I'd like to specify time here. 

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

multi_array = np.asarray([[1, 1, 10, 100, 1000], [10000, 1000, 100, 10, 1], [300, 400, 5000, 12, 47]])
log_array=np.log10(multi_array)
s = plt.scatter(x, y, c=log_array[time], marker = 's',s=100)
cb = plt.colorbar(s)
cb.set_label('log of ...')
plt.show()

enter image description here

答案 1 :(得分:0)

经过一些修补,并使用从用户4421975的答案和评论中的链接中获得的信息,我一直困惑不解。简而言之,我使用plt.scatter的标准功能/属性/ thingie来弄乱颜色并使它们成为对数。

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(1)

time = 2 

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

multi_array = np.asarray([[1, 1, 10, 100, 1000], [10000, 1000, 100, 10, 1], [300, 400, 5000, 12, 47]])

for counter in np.arange(0, 5):
    s = plt.scatter(x[counter], y[counter], c = multi_array[time, counter], cmap = 'winter', norm = matplotlib.colors.LogNorm(vmin=multi_array[time].min(), vmax=multi_array[time].max()), marker = 's', )

cb = plt.colorbar(s)
cb.set_label('Log of Data')

plt.show()