2013-03-10 7 views
6

मैं matplotlib साजिश की उत्पत्ति पर त्रिज्या आर का आधा काला, आधा सफेद सर्कल रखना चाहता हूं। मुझे पता है कि Circle class मौजूद है, लेकिन मुझे नहीं पता कि सर्कल का बायां आधा सफेद होना चाहिए और दायां आधा काला होना चाहिए। (एक आदर्श समाधान मुझे सर्कल के अभिविन्यास को निर्दिष्ट करने की अनुमति देगा - उदाहरण के लिए मुझे इसे घुमाने में सक्षम होना चाहिए ताकि उदाहरण के लिए शीर्ष सफेद हो और नीचे का काला हो)।Matplotlib आधा काला और आधा सफेद सर्कल

+0

आप दो 'Wedge's साथ यह करने के लिए सक्षम होना चाहिए, लेकिन मैं मुसीबत हो रही बातें ठीक से काम करने आ रही हैं ... –

+0

@JoeKington - ओह। यदि आप इसे काम करने के लिए प्रबंधित नहीं कर सकते हैं, तो यह मेरे लिए थोड़ा हतोत्साहित है ;-) (मैंने आपके कुछ matplotlib को यहां SO पर देखा है ... यह बहुत प्रभावशाली है) – mgilson

+0

धन्यवाद! बाहर निकलता है मैं बस कुछ बेवकूफ कर रहा था। यदि आप चीजों को यादृच्छिक टाइपो नहीं जोड़ते हैं तो दो wedges पूरी तरह से काम करता है! –

उत्तर

14

दो Wedge एस का उपयोग करने का सबसे आसान तरीका है। एक त्वरित उदाहरण के रूप में (यह स्वचालित रूप से कुल्हाड़ियों rescale नहीं है, लेकिन है कि, जोड़ना आसान है, यदि आप चाहें।)

:

import matplotlib.pyplot as plt 
from matplotlib.patches import Wedge 

def main(): 
    fig, ax = plt.subplots() 
    dual_half_circle((0.5, 0.5), radius=0.3, angle=90, ax=ax) 
    ax.axis('equal') 
    plt.show() 

def dual_half_circle(center, radius, angle=0, ax=None, colors=('w','k'), 
        **kwargs): 
    """ 
    Add two half circles to the axes *ax* (or the current axes) with the 
    specified facecolors *colors* rotated at *angle* (in degrees). 
    """ 
    if ax is None: 
     ax = plt.gca() 
    theta1, theta2 = angle, angle + 180 
    w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs) 
    w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs) 
    for wedge in [w1, w2]: 
     ax.add_artist(wedge) 
    return [w1, w2] 

main() 

enter image description here

आप इसे करना चाहते हैं तो हमेशा उत्पत्ति पर रहने के लिए, आप ट्रांसफॉर्म को ax.transAxes होने के लिए निर्दिष्ट कर सकते हैं, और क्लिपिंग बंद कर सकते हैं।

उदा।

import matplotlib.pyplot as plt 
from matplotlib.patches import Wedge 

def main(): 
    fig, ax = plt.subplots() 
    dual_half_circle(radius=0.1, angle=90, ax=ax) 
    ax.axis('equal') 
    plt.show() 

def dual_half_circle(radius, angle=0, ax=None, colors=('w','k'), **kwargs): 
    """ 
    Add two half circles to the axes *ax* (or the current axes) at the lower 
    left corner of the axes with the specified facecolors *colors* rotated at 
    *angle* (in degrees). 
    """ 
    if ax is None: 
     ax = plt.gca() 
    kwargs.update(transform=ax.transAxes, clip_on=False) 
    center = (0, 0) 
    theta1, theta2 = angle, angle + 180 
    w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs) 
    w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs) 
    for wedge in [w1, w2]: 
     ax.add_artist(wedge) 
    return [w1, w2] 

main() 

हालांकि, यह सर्कल की "परिपत्र" अक्षों की रूपरेखा के पहलू अनुपात पर निर्भर करेगा। (आप इसे दो तरीकों से प्राप्त कर सकते हैं, लेकिन यह अधिक जटिल हो जाता है। मुझे बताएं कि क्या आपके मन में यह है और मैं एक और विस्तृत उदाहरण दिखा सकता हूं।) मैंने यह भी गलत समझा होगा कि "मूल में "।

+0

हाँ। यह वही है जो मैं चाहता हूं। धन्यवाद :) – mgilson

+0

कोई समस्या नहीं है! आशा करता हूँ की ये काम करेगा! –

+0

add_artist मेरे लिए काम नहीं करता है। मुझे add_patch का उपयोग करना है। यह 1.4.2 पर काम करता है और मैंने इसे पहले के संस्करणों पर भी इस्तेमाल किया है। –

5

यदि आपके पास इस प्रतीक के साथ एक फ़ॉन्ट है तो आप यूनिकोड आधे भरे सर्कल (यू +25 डी 0) का उपयोग कर सकते हैं। विचित्र रूप से, यह STIX (matplotlib के साथ शामिल) में नहीं है, लेकिन मुझे पता है कि यह देजावु सांस में है, इसलिए मैं इसे वहां से उपयोग करूंगा।

enter image description here

import matplotlib.pyplot as plt 
import matplotlib.font_manager 
from numpy import * 

path = '/full/path/to/font/DejaVuSans.ttf' 
f0 = matplotlib.font_manager.FontProperties()  
f0.set_file(path) 

plt.figure() 
plt.xlim(-1.2,1.2) 
plt.ylim(-1.2,1.2) 

for angle in arange(0, 2*pi, 2*pi/10): 
    x, y = cos(angle), sin(angle) 
    plt.text(x, y, u'\u25D0', fontproperties=f0, rotation=angle*(180/pi), size=30) 

plt.show() 
+0

यह साफ है, लेकिन आप अक्ष इकाइयों में मंडलियों का आकार निर्दिष्ट नहीं कर सकते हैं जो मैं वास्तव में यहां चाहता हूं। – mgilson

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