2012-08-16 8 views
53

काम नहीं कर रहा कभी matplotlib मैं निम्न त्रुटि जब भी लीजेंड बनाने की कोशिश कर पाने के उन्नयन के बाद से:matplotlib महापुरूष

import matplotlib.pyplot as plt 

a = [1,2,3] 
b = [4,5,6] 
c = [7,8,9] 

plot1 = plt.plot(a,b) 
plot2 = plt.plot(a,c) 

plt.legend([plot1,plot2],["plot 1", "plot 2"]) 
plt.show() 

मैंने:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30810>] 
Use proxy artist instead. 

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist 

    warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),)) 
/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30990>] 
Use proxy artist instead. 

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist 

    warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),)) 

यह भी इस तरह एक छोटी सी स्क्रिप्ट के साथ होता है त्रुटि मिली है कि त्रुटि मुझे त्रुटि के स्रोत का निदान करने में काफी बेकार की ओर इंगित करती है।

उत्तर

107

आप जोड़ना चाहिए अल्पविराम का:

plot1, = plt.plot(a,b) 
plot2, = plt.plot(a,c) 

क्योंकि plt.plot() कोई फर्क नहीं पड़ता कि कितने वास्तव में आदेश से बनाई गई हैं लाइन वस्तुओं, के एक टपल रिटर्न कारण यदि आप कॉमा की जरूरत है। अल्पविराम के बिना, "प्लॉट 1" और "प्लॉट 2" लाइन ऑब्जेक्ट्स के बजाए टुपल्स हैं, जो बाद में कॉल plt.legend() विफल हो जाते हैं।

कॉमा परिणाम को अनपेक्षित रूप से अनपैक करता है ताकि एक टुपल के बजाय, "प्लॉट 1" और "प्लॉट 2" स्वचालित रूप से टुपल के भीतर पहली वस्तु बन जाए, यानी लाइन ऑब्जेक्ट्स जो आप वास्तव में चाहते हैं।

http://matplotlib.sourceforge.net/users/legend_guide.html#adjusting-the-order-of-legend-items

line, = plot(x,sin(x)) what does comma stand for?

+7

यही काम किया, सुंदर रहस्यमय सामान! –

+2

क्या आप यहां स्पष्टीकरण कॉपी/जोड़ सकते हैं? stackoverflow साइट पर प्रासंगिक भागों की प्रतिलिपि को प्रोत्साहित करता है (हाइलाइटिंग, संग्रह) – n611x007

5

उपयोग handles उर्फ ​​Proxy artists

import matplotlib.lines as mlines 
import matplotlib.pyplot as plt 

blue_line = mlines.Line2D([], [], color='blue', label='My Label') 
reds_line = mlines.Line2D([], [], color='reds', label='My Othes') 

plt.legend(handles=[blue_line, reds_line]) 

plt.show() 
0

उपयोग "लेबल" कीवर्ड, इसलिए जैसे:

pyplot.plot(x, y, label='x vs. y') 

और उसके बाद तो जैसे कथा जोड़ें:

pyplot.legend() 

कथा मोटाई, रंग की तरह लाइन गुण रख सकेंगे, आदि

enter image description here

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

  • कोई संबंधित समस्या नहीं^_^