2011-08-12 12 views
33

में स्वचालित रूप से टेक्स्ट बॉक्स को स्थिति में रखें pyplot.text() को pyplot.legend() के साथ एक स्थान जैसा कहने का कोई तरीका है?matplotlib

कुछ कथा तर्क की तरह उत्कृष्ट होगा:

plt.legend(loc="upper left") 

मैं पत्र (जैसे 'ए', 'बी') का उपयोग कर विभिन्न कुल्हाड़ियों साथ subplots लेबल करने के लिए कोशिश कर रहा हूँ। मुझे लगता है कि स्थिति का मैन्युअल अनुमान लगाने से बेहतर तरीका होना चाहिए।

धन्यवाद

उत्तर

20

मुझे यकीन नहीं है कि यह मूल रूप से प्रश्न पोस्ट करते समय उपलब्ध था, लेकिन स्थानीय पैरामीटर का उपयोग करके अब वास्तव में उपयोग किया जा सकता है। धन्यवाद -

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.offsetbox import AnchoredText 

# make some data 
x = np.arange(10) 
y = x 

# set up figure and axes 
f, ax = plt.subplots(1,1) 

# loc works the same as it does with figures (though best doesn't work) 
# pad=5 will increase the size of padding between the border and text 
# borderpad=5 will increase the distance between the border and the axes 
# frameon=False will remove the box around the text 

anchored_text = AnchoredText("Test", loc=2) 
ax.plot(x,y) 
ax.add_artist(anchored_text) 

plt.show() 

enter image description here

+1

'एंकरर्डटेक्स्ट' 'loc = "सर्वोत्तम' 'को संभालने के लिए प्रतीत नहीं होता है? – zyxue

+0

@zyxue: https://github.com/matplotlib/matplotlib/issues/1313 देखें – naught101

31

बस annotate का उपयोग करें और अक्ष निर्देशांक निर्दिष्ट करें। उदाहरण के लिए, "ऊपरी बाएँ" होगा:

plt.annotate('Something', xy=(0.05, 0.95), xycoords='axes fraction') 

तुम भी अधिक सजावटी मिल सकता है और एक निरंतर अंक में ऑफसेट निर्दिष्ट करें:

plt.annotate('Something', xy=(0, 1), xytext=(12, -12), va='top' 
      xycoords='axes fraction', textcoords='offset points') 

अधिक विवरण के लिए उदाहरण here और अधिक विस्तृत उदाहरण देखें here

+0

@Garrett: नीचे एक उदाहरण है! उन्हें अब तय किया जाना चाहिए। –