2013-06-24 25 views
8

私はいくつかの閉じたポリゴンを作成するためにpythonとmatplotlibを使用しています。私はその後、ハッチでそれらを埋める必要があります。これはset_hatchで行うことができます。 matplotlibのカスタムハッチでポリゴンを塗りつぶす方法は?

http://matplotlib.org/examples/pylab_examples/hatch_demo.html

http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch.set_hatch

は、残念ながら、私はグレースケール画像で働いている、と私は、デフォルトで提供されるよりも多くのハッチを必要とする - 私が代わりにタイル状にすることができ、ビットマップ(またはいくつかの類似した画像)を提供することを好むだろうこれらのハッチを様々な密度で使用すること。

私は他のPythonライブラリ(pyglet、pygame、PILなど)を公開していますが、私はPythonで解決することをお勧めします。

+0

[カスタムハッチ]の例があります(http://stackoverflow.com/questions/4745937/how-to-decrease- hatch-density-in-matplotlib?rq = 1)ここで、著者はそれが脆いと言っています。 – cphlewis

+1

標準のset_hatchには8つの異なるハッチングがあり、それぞれが少なくとも2つの密度で動作し、組み合わせることができます。私は、あなたがハッチの組み合わせを使い果たす前に、プロットがあまりにも混乱すると思うでしょう。たくさんの使用可能な塗りつぶしでグレースケールの孵化の例がありますか? – cphlewis

答えて

6

matplotlib.hatch.Shapesをサブクラス化し、単位平方[[-0.5、0.5] x [-0.5、0.5]]で描画された参照パスに基づいてカスタムハッチングを定義できます。

仮称:

import numpy as np 
import matplotlib.hatch 
import matplotlib.pyplot as plt 
from matplotlib.patches import Ellipse, Polygon 


house_path = Polygon(
    [[-0.3, -0.4], [0.3, -0.4], [0.3, 0.1], [0., 0.4], [-0.3, 0.1]], 
    closed=True, fill=False).get_path() 

class CustomHatch(matplotlib.hatch.Shapes): 
    """ 
    Custom hatches defined by a path drawn inside [-0.5, 0.5] square. 
    Identifier 'c'. 
    """ 
    filled = True 
    size = 1.0 
    path = house_path 

    def __init__(self, hatch, density): 
     self.num_rows = (hatch.count('c')) * density 
     self.shape_vertices = self.path.vertices 
     self.shape_codes = self.path.codes 
     matplotlib.hatch.Shapes.__init__(self, hatch, density) 

matplotlib.hatch._hatch_types.append(CustomHatch) 

fig = plt.figure() 
ax = fig.add_subplot(111) 

ellipse = ax.add_patch(Ellipse((0.5, 0.5), 0.3, 0.5, fill=False)) 
ellipse.set_hatch('c') 
ellipse.set_color('red') 
plt.show() 

寄付:

enter image description here

関連する問題