使用 matplotlib 设置数字的个性化色图图
Personalised colourmap plot using set numbers using matplotlib
我有一个数据看起来像(示例)
x y d
0 0 -2
1 0 0
0 1 1
1 1 3
我想把它变成一个颜色图,看起来像以下之一:
其中 x 和 y 在 table 中,颜色由 'd' 给出。但是,我想要每个数字的预定颜色,例如:
-2 - orange
0 - blue
1 - red
3 - yellow
不一定是这些颜色,但我需要将一个数字分配给一种颜色,这些数字没有顺序或顺序,它们只是一组五个或六个随机数,它们在整个数组中重复出现。
任何想法,我没有代码,因为我不知道从哪里开始。但是,我已经查看了此处的示例,例如:
Matplotlib python change single color in colormap
但是它们只展示了如何定义颜色,而不是如何将这些颜色 link 设置为特定值。
事实证明这比我想象的要难,所以也许有人有更简单的方法。
由于我们需要创建数据图像,因此我们将它们存储在二维数组中。然后我们可以将数据映射到整数 0 .. number of different data values
并为每个整数分配一种颜色。原因是我们希望最终的颜色图是等间距的。所以
值-2
--> 整数0
--> 颜色orange
值 0
--> 整数 1
--> 颜色 blue
等等。
有了很好间隔的整数,我们可以在新创建的整数值的图像上使用 ListedColormap
。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
# define the image as a 2D array
d = np.array([[-2,0],[1,3]])
# create a sorted list of all unique values from d
ticks = np.unique(d.flatten()).tolist()
# create a new array of same shape as d
# we will later use this to store values from 0 to number of unique values
dc = np.zeros(d.shape)
#fill the array dc
for i in range(d.shape[0]):
for j in range(d.shape[1]):
dc[i,j] = ticks.index(d[i,j])
# now we need n (= number of unique values) different colors
colors= ["orange", "blue", "red", "yellow"]
# and put them to a listed colormap
colormap = matplotlib.colors.ListedColormap(colors)
plt.figure(figsize=(5,3))
#plot the newly created array, shift the colorlimits,
# such that later the ticks are in the middle
im = plt.imshow(dc, cmap=colormap, interpolation="none", vmin=-0.5, vmax=len(colors)-0.5)
# create a colorbar with n different ticks
cbar = plt.colorbar(im, ticks=range(len(colors)) )
#set the ticklabels to the unique values from d
cbar.ax.set_yticklabels(ticks)
#set nice tickmarks on image
plt.gca().set_xticks(range(d.shape[1]))
plt.gca().set_yticks(range(d.shape[0]))
plt.show()
由于直观上可能不清楚如何获得数组 d
以使用 imshow 绘图所需的形状,即作为二维数组,这里有两种转换输入数据列的方法:
import numpy as np
x = np.array([0,1,0,1])
y = np.array([ 0,0,1,1])
d_original = np.array([-2,0,1,3])
#### Method 1 ####
# Intuitive method.
# Assumption:
# * Indexing in x and y start at 0
# * every index pair occurs exactly once.
# Create an empty array of shape (n+1,m+1)
# where n is the maximum index in y and
# m is the maximum index in x
d = np.zeros((y.max()+1 , x.max()+1), dtype=np.int)
for k in range(len(d_original)) :
d[y[k],x[k]] = d_original[k]
print d
#### Method 2 ####
# Fast method
# Additional assumption:
# indizes in x and y are ordered exactly such
# that y is sorted ascendingly first,
# and for each index in y, x is sorted.
# In this case the original d array can bes simply reshaped
d2 = d_original.reshape((y.max()+1 , x.max()+1))
print d2
我有一个数据看起来像(示例)
x y d
0 0 -2
1 0 0
0 1 1
1 1 3
我想把它变成一个颜色图,看起来像以下之一:
其中 x 和 y 在 table 中,颜色由 'd' 给出。但是,我想要每个数字的预定颜色,例如:
-2 - orange
0 - blue
1 - red
3 - yellow
不一定是这些颜色,但我需要将一个数字分配给一种颜色,这些数字没有顺序或顺序,它们只是一组五个或六个随机数,它们在整个数组中重复出现。
任何想法,我没有代码,因为我不知道从哪里开始。但是,我已经查看了此处的示例,例如:
Matplotlib python change single color in colormap
但是它们只展示了如何定义颜色,而不是如何将这些颜色 link 设置为特定值。
事实证明这比我想象的要难,所以也许有人有更简单的方法。
由于我们需要创建数据图像,因此我们将它们存储在二维数组中。然后我们可以将数据映射到整数 0 .. number of different data values
并为每个整数分配一种颜色。原因是我们希望最终的颜色图是等间距的。所以
值-2
--> 整数0
--> 颜色orange
值 0
--> 整数 1
--> 颜色 blue
等等。
有了很好间隔的整数,我们可以在新创建的整数值的图像上使用 ListedColormap
。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
# define the image as a 2D array
d = np.array([[-2,0],[1,3]])
# create a sorted list of all unique values from d
ticks = np.unique(d.flatten()).tolist()
# create a new array of same shape as d
# we will later use this to store values from 0 to number of unique values
dc = np.zeros(d.shape)
#fill the array dc
for i in range(d.shape[0]):
for j in range(d.shape[1]):
dc[i,j] = ticks.index(d[i,j])
# now we need n (= number of unique values) different colors
colors= ["orange", "blue", "red", "yellow"]
# and put them to a listed colormap
colormap = matplotlib.colors.ListedColormap(colors)
plt.figure(figsize=(5,3))
#plot the newly created array, shift the colorlimits,
# such that later the ticks are in the middle
im = plt.imshow(dc, cmap=colormap, interpolation="none", vmin=-0.5, vmax=len(colors)-0.5)
# create a colorbar with n different ticks
cbar = plt.colorbar(im, ticks=range(len(colors)) )
#set the ticklabels to the unique values from d
cbar.ax.set_yticklabels(ticks)
#set nice tickmarks on image
plt.gca().set_xticks(range(d.shape[1]))
plt.gca().set_yticks(range(d.shape[0]))
plt.show()
由于直观上可能不清楚如何获得数组 d
以使用 imshow 绘图所需的形状,即作为二维数组,这里有两种转换输入数据列的方法:
import numpy as np
x = np.array([0,1,0,1])
y = np.array([ 0,0,1,1])
d_original = np.array([-2,0,1,3])
#### Method 1 ####
# Intuitive method.
# Assumption:
# * Indexing in x and y start at 0
# * every index pair occurs exactly once.
# Create an empty array of shape (n+1,m+1)
# where n is the maximum index in y and
# m is the maximum index in x
d = np.zeros((y.max()+1 , x.max()+1), dtype=np.int)
for k in range(len(d_original)) :
d[y[k],x[k]] = d_original[k]
print d
#### Method 2 ####
# Fast method
# Additional assumption:
# indizes in x and y are ordered exactly such
# that y is sorted ascendingly first,
# and for each index in y, x is sorted.
# In this case the original d array can bes simply reshaped
d2 = d_original.reshape((y.max()+1 , x.max()+1))
print d2