2016-11-30 4 views
0

を使用してセット番号を使用してcolourmapプロットをパーソナライズ私は(例)のように見えるデータを持ってmatplotlibの

x y d 
0 0 -2 
1 0 0 
0 1 1 
1 1 3 

そして、私はこれらのいずれかのように見えるcoloumapプロットにこれを有効にする:

enter image description here

ここで、xとyは表にあり、色は 'd'で与えられます。これらの色は必ずしも

-2 - orange 
0 - blue 
1 - red 
3 - yellow 

ないが、私は色に番号を対処する必要があると数字が順番またはシーケンスではありませんが、インクルードだけで設定されています。しかし、私は、例えば、各番号の所定の色をしたいです配列全体にわたって自分自身を繰り返す5つまたは6つの乱数からなる。

私はどこから始めたらいいのか分からないので、アイデアはありません。彼らは唯一の色を定義する方法と、特定の値にこれらの色をリンクする方法を示していないが

Matplotlib python change single color in colormap

:私は、しかし、のような、ここでは例を見てきました。

答えて

1

これは私が思ったよりも難しいことが判明したので、誰かがこれをやる方が簡単かもしれません。

データのイメージを作成する必要があるので、それらを2次元配列に格納します。次に、データを整数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() 

enter image description here


すなわち2次元配列として、ここで入力を変換する2つの方法があり、関数imshowをプロットするために必要な形状で配列dを取得する方法を直感的に明らかではないかもしれないとして、データ列:

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 
+0

私はやりたいこととまったく同じです。しかし、ただ一つの質問。私のデータは長い数字のリスト(100のようなもの)です。あなたは使用していなかったxとyの位置を持っています。しかし、どのように私は2D配列に値の長いリストを使用しますか?それはあなたのdでしょうか? – Mac

+0

配列を作成する必要があると仮定しています。データが10x10の配列を必要とする10x10の色のグラフを表示するとしたら、それは正確ですか? – Mac

+1

私はあなたが持っている列構造から必要な配列を作る方法を含める答えを編集しました。実際には、10x10グラフをプロットするには、10x10の配列が必要です。索引付けは暗黙的に行われ、その配列の最初の列、最初の行は索引0,0です。最後の列、最後の行はインデックス9,9を持ちます。 – ImportanceOfBeingErnest

関連する問題