2017-06-23 7 views
0

私は10,000 + Matplotlib Polygonオブジェクトのリストを持っています。各ポリゴンは20のグループのうちの1つに属します。各ユニークなグループをユニークな色にマッピングすることによって、ポリゴンが属するグループを区別したいと思います。ポリゴンの色を設定するMatplotlib

Fill polygons with multiple colors in python matplotlib

How to fill polygons with colors based on a variable in Matplotlib?

How do I set color to Rectangle in Matplotlib?

これらのソリューションは、単にリスト内の各形状にランダムな色を適用します。

ここで私は私に同様の問題を発見したいくつかの記事があります。それは私が探しているものではありません。私にとっては、特定のグループに属する各図形は同じ色でなければなりません。何か案は?

サンプル・コード:このコードを実行する場合poly_verticesとpoly_colorsが定義されていないので、それが動作しないこと

from matplotlib.collections import PatchCollection 
from matplotlib.patches import Polygon 
from matplotlib import pyplot as plt 

patches = [] 
colors = [] 
num_polys = 10000 
for i in range(num_polys): 
    patches.append(Polygon(poly_vertices[i], closed=True)) 
    colors.append(poly_colors[i]) # This line is pointless, but I'm letting you know that 
            # I have a particular color for each polygon 

fig, ax = plt.subplots() 
p = PatchCollection(patches, alpha=0.25) 
ax.add_collection(p) 
ax.autoscale() 
plt.show() 

は注意してください。ここでは、poly_verticesはポリゴンの頂点のリストであり、poly_colorsはRGBの色のリストであり、各リストには10​​000のエントリがあると仮定します。例えば

:poly_vertices [0] = [(0,0)、(1,0)、(0,1)]、色[0] = [1、0,0]

ありがとう!

答えて

-1

さて、私は何をしようとしているのか分かりました。私は、同様の問題を抱えている可能性のある人のために答えを投稿します。

何らかの理由で、ポリゴン自体の色を設定しても機能しません。すなわち

Polygon(vertices, color=[1, 0, 0]) 

は機能しません。代わりに

p = PatchCollection(patches) 
p.set_color([1, 0, 0]) 

を使用し、コレクションにすべてのポリゴンを追加した後、しかし、私はまだ色によってグループの多角形にしたいです。したがって、私は複数のPatchCollectionを追加する必要があります - 各グループタイプに1つ!

など

ポリゴンの私の元のリストは順不同でいたので、その隣人はグループ1に属しながら、最初のポリゴンが、グループ5に属していてもよい、

だから、私は最初、このようなグループ番号でリストを並べ替え特定のグループに属するすべてのポリゴンが互いに隣り合っていることを示します。

次にソートされたリストを繰り返し、各ポリゴンを一時的なリストに追加しました。新しいグループタイプに達すると、一時リストのすべてのポリゴンを自分のPatchCollectionに追加することができたことが分かりました。ここで

は、このロジックのためのいくつかのコードです:

a = [x for x in original_groups]         # The original group numbers (unsorted) 
idx = sorted(range(len(a)), key=lambda k: a[k])     # Get indices of sorted group numbers 
current_group = original_groups[idx[0]]       # Set the current group to the be the first sorted group number 
temp_patches = []             # Create a temporary patch list 

for i in idx:              # iterate through the sorted indices 
    if current_group == original_groups[i]:      # Detect whether a change in group number has occured 
     temp_patches.append(original_patches[i])     # Add patch to the temporary variable since group number didn't change 
    else: 
     p = PatchCollection(temp_patches, alpha=0.6)    # Add all patches belonging to the current group number to a PatchCollection 
     p.set_color([random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]) # Set all shapes belonging to this group to the same random color 
     ax.add_collection(p)          # Add all shapes belonging this group to the axes object 
     current_group = original_groups[i]      # The group number has changed, so update the current group number 
     temp_patches = [original_patches[i]]      # Reset temp_patches, to begin collecting patches of the next group number         

p = PatchCollection(temp_patches, alpha=0.6)      # temp_patches currently contains the patches belonging to the last group. Add them to a PatchCollection 
p.set_color([random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]) 
ax.add_collection(p)       

ax.autoscale()             # Default scale may not capture the appropriate region 
plt.show() 
関連する問題