Matplotlib लेजेंड में तत्वों का क्रम कैसे बदलें


आप Matplotlib लेजेंड में तत्वों का क्रम बदलने के लिए निम्नलिखित कोड का उपयोग कर सकते हैं:

 #get handles and labels
handles, labels = plt. gca (). get_legend_handles_labels ()

#specify order of items in legend
order = [1,2,0]

#add legend to plot
plt. legend ([handles[idx] for idx in order],[labels[idx] for idx in order])

निम्नलिखित उदाहरण दिखाता है कि व्यवहार में इस वाक्यविन्यास का उपयोग कैसे करें।

उदाहरण: Matplotlib लेजेंड में तत्वों का क्रम बदलें

मान लीजिए कि हम Matplotlib में निम्नलिखित लाइन चार्ट बनाते हैं:

 import pandas as pd
import matplotlib. pyplot as plt

#create data
df = pd. DataFrame ({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8],
                   'rebounds': [6, 8, 8, 10, 14, 12, 12, 10, 11]})

#add lines to plot
plt. plot (df['points'], label='Points', color='green')
plt. plot (df['assists'], label='Assists', color='steelblue')
plt. plot (df['rebounds'], label='Rebounds', color='purple')

#add legend
plt. legend () 

किंवदंती तत्वों को उसी क्रम में रखा गया है जिसमें हमने कथानक में पंक्तियाँ जोड़ी हैं।

हालाँकि, हम लेजेंड में तत्वों के क्रम को अनुकूलित करने के लिए निम्नलिखित सिंटैक्स का उपयोग कर सकते हैं:

 import pandas as pd
import matplotlib. pyplot as plt

#create data
df = pd. DataFrame ({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8],
                   'rebounds': [6, 8, 8, 10, 14, 12, 12, 10, 11]})

#add lines to plot
plt. plot (df['points'], label='Points', color='green')
plt. plot (df['assists'], label='Assists', color='steelblue')
plt. plot (df['rebounds'], label='Rebounds', color='purple')

#get handles and labels
handles, labels = plt. gca (). get_legend_handles_labels ()

#specify order of items in legend
order = [1,2,0]

#add legend to plot
plt. legend ([handles[idx] for idx in order],[labels[idx] for idx in order]) 

माटप्लोटलिब किंवदंतियों का क्रम

ध्यान दें कि हमने निर्दिष्ट किया है:

  • क्रम = [1, 2, 0]

इसका मत:

  • लेजेंड में पहला आइटम वह लेबल होना चाहिए जो मूल रूप से पुराने लेजेंड (“मदद”) के सूचकांक स्थान 1 पर था।
  • लेजेंड का दूसरा तत्व वह लेबल होना चाहिए जो मूल रूप से पुराने लेजेंड (“बाउंस”) के सूचकांक स्थान 2 पर था
  • लेजेंड का तीसरा तत्व वह लेबल होना चाहिए जो मूल रूप से पुराने लेजेंड (“अंक”) के सूचकांक स्थिति 0 पर था

अतिरिक्त संसाधन

निम्नलिखित ट्यूटोरियल बताते हैं कि मैटप्लोटलिब में अन्य सामान्य ऑपरेशन कैसे करें:

Matplotlib में लेजेंड की स्थिति कैसे बदलें
मैटप्लोटलिब प्लॉट के बाहर लेजेंड को कैसे रखें
Matplotlib में लेजेंड फ़ॉन्ट आकार कैसे बदलें

एक टिप्पणी जोड़ने

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *