2013-06-24 7 views
8

मैं कई बंद बहुभुज बनाने के लिए अजगर और matplotlib का उपयोग कर रहा हूँ। तब मुझे उन्हें एक हैच से भरने की ज़रूरत है, जिसे set_hatch के माध्यम से किया जा सकता है।matplotlib में कस्टम हैच के साथ बहुभुज को कैसे भरें?

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

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

दुर्भाग्य से मैं ग्रेस्केल छवियों के साथ काम कर रहा हूँ, और मैं अधिक hatches डिफ़ॉल्ट रूप से प्रदान की तुलना में की जरूरत है - मैं एक बिटमैप (या कुछ इसी तरह की छवि) जो बजाय टाइलों किया जा सकता है प्रदान करने के लिए पसंद करेंगे विभिन्न घनत्व के साथ इन hatches का उपयोग करने के लिए।

मैं अन्य पायथन पुस्तकालयों (पायगलेट, पायगम, पीआईएल, आदि) के लिए खुला हूं, हालांकि मैं अजगर में होने का समाधान पसंद करूंगा।

+0

की [कस्टम hatches] एक उदाहरण (नहीं है http://stackoverflow.com/questions/4745937/how-to-decrease- हैच-घनत्व-में-matplotlib? rq = 1) यहां, लेकिन लेखक कहते हैं कि यह भंगुर है। – cphlewis

+1

मानक set_hatch में आठ अलग-अलग हैंच हैं, जिनमें से प्रत्येक कम से कम दो घनत्वों में काम कर सकता है, और जिसे जोड़ा जा सकता है। मुझे लगता है कि टोपी के संयोजन से बाहर होने से पहले एक साजिश बहुत भ्रमित हो जाएगी। क्या आपके पास दर्जनों उपयोग योग्य भरने के साथ ग्रेस्केल हैचिंग का एक उदाहरण है? – cphlewis

उत्तर

6

आप matplotlib.hatch.Shapes उपclass कर सकते हैं और इकाई वर्ग [[-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

संबंधित मुद्दे