के साथ क्षेत्रों के पूरक भरना मैं वर्तमान में पाइथन और matplotlib के साथ कुछ लागू कर रहा हूँ। मुझे पता है कि बहुभुज कैसे आकर्षित करें और उन्हें कैसे भरें, लेकिन मैं को सब कुछ कैसे भर सकता हूं बहुभुज के इंटीरियर को छोड़कर? स्पष्ट होने के लिए, मैं नीचे दिए गए परिणाम को संशोधित करना चाहता हूं, axhspan
और axvspan
का उपयोग कर क्षैतिज और ऊर्ध्वाधर लाल रेखाओं को क्लिप करके, एक लाल आयताकार प्राप्त करने के लिए प्राप्त किया है (जिसके बाहर सबकुछ अब जैसा है) : matplotlib
7
A
उत्तर
4
This post पूछता है (और उत्तर) अनिवार्य रूप से इस सवाल का। स्वीकृत उत्तर में 'संपादित करें 2' देखें। यह वर्णन करता है कि वेक्टर पॉलीगॉन को अपनी साजिश सीमाओं के आकार को कैसे बनाया जाए और फिर उस आकार से मेल खाने के लिए इसमें एक छेद कैसे बनाया जाए, जिसे आप पूरक बनाना चाहते हैं। यह लाइन कोड असाइन करके करता है जो परिभाषित करता है कि जब पेन चलता है तो पेन खींचता है या नहीं।
import numpy as np
import matplotlib.pyplot as plt
def main():
# Contour some regular (fake) data
grid = np.arange(100).reshape((10,10))
plt.contourf(grid)
# Verticies of the clipping polygon in counter-clockwise order
# (A triange, in this case)
poly_verts = [(2, 2), (5, 2.5), (6, 8), (2, 2)]
mask_outside_polygon(poly_verts)
plt.show()
def mask_outside_polygon(poly_verts, ax=None):
"""
Plots a mask on the specified axis ("ax", defaults to plt.gca()) such that
all areas outside of the polygon specified by "poly_verts" are masked.
"poly_verts" must be a list of tuples of the verticies in the polygon in
counter-clockwise order.
Returns the matplotlib.patches.PathPatch instance plotted on the figure.
"""
import matplotlib.patches as mpatches
import matplotlib.path as mpath
if ax is None:
ax = plt.gca()
# Get current plot limits
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Verticies of the plot boundaries in clockwise order
bound_verts = [(xlim[0], ylim[0]), (xlim[0], ylim[1]),
(xlim[1], ylim[1]), (xlim[1], ylim[0]),
(xlim[0], ylim[0])]
# A series of codes (1 and 2) to tell matplotlib whether to draw a line or
# move the "pen" (So that there's no connecting line)
bound_codes = [mpath.Path.MOVETO] + (len(bound_verts) - 1) * [mpath.Path.LINETO]
poly_codes = [mpath.Path.MOVETO] + (len(poly_verts) - 1) * [mpath.Path.LINETO]
# Plot the masking patch
path = mpath.Path(bound_verts + poly_verts, bound_codes + poly_codes)
patch = mpatches.PathPatch(path, facecolor='white', edgecolor='none')
patch = ax.add_patch(patch)
# Reset the plot limits to their original extents
ax.set_xlim(xlim)
ax.set_ylim(ylim)
return patch
if __name__ == '__main__':
main()
1
यदि आपको केवल आयत के पूरक की आवश्यकता है, तो आप इसके चारों ओर 4 आयतों को आकर्षित कर सकते हैं (जैसे कि 4 आयत जो आपकी उदाहरण छवि में दिखाई दे रहे हैं)। साजिश किनारों के निर्देशांक xlim()
और ylim()
के साथ प्राप्त किए जा सकते हैं।
मुझे यकीन है कि matplotlib एक बहुभुज के बाहर चित्रकला का एक तरीका प्रदान करता है नहीं कर रहा हूँ ...
संबंधित मुद्दे
- 1. matplotlib
- 2. matplotlib
- 3. matplotlib
- 4. matplotlib
- 5. Matplotlib
- 6. matplotlib
- 7. matplotlib
- 8. matplotlib
- 9. matplotlib
- 10. matplotlib
- 11. matplotlib
- 12. matplotlib
- 13. matplotlib
- 14. matplotlib
- 15. matplotlib
- 16. MatPlotLib
- 17. Matplotlib
- 18. matplotlib?
- 19. matplotlib
- 20. matplotlib
- 21. matplotlib
- 22. matplotlib
- 23. matplotlib
- 24. matplotlib
- 25. matplotlib
- 26. matplotlib
- 27. matplotlib
- 28. matplotlib
- 29. matplotlib
- 30. matplotlib
पोस्ट आप का उल्लेख एक 2D पिक्सेल छवि मास्किंग के बारे में है:
यहाँ ऊपर संदर्भित पोस्ट के भाग कि इस प्रश्न के लिए प्रासंगिक है। हालांकि यह सवाल का अनुमानित समाधान हो सकता है, आदर्श समाधान वेक्टर ड्राइंग (पिक्सेल ड्राइंग नहीं) के माध्यम से होगा। – EOL
स्वीकार्य उत्तर के 'एडिड 2' तक स्क्रॉल करें, आपको एक वेक्टर समाधान मिलेगा। – Paul
धन्यवाद bpowah! दरअसल, 'संपादन 2' में 'mask_outside_polygon' फ़ंक्शन कुछ हचिंग के साथ चाल चल रहा था। –