सीबॉर्न बारप्लॉट में बार का रंग कैसे सेट करें


आप समुद्री बारप्लॉट में बार का रंग सेट करने के लिए निम्नलिखित विधियों का उपयोग कर सकते हैं:

विधि 1: सभी बारों के लिए रंग सेट करें

 #use steelblue for the color of all bars
sns. barplot (x=xvar, y=yvar, color=' steelblue ')

विधि 2: अधिकतम मान के साथ बार रंग सेट करें

 #use orange for bar with max value and gray for all other bars
cols = [' gray ' if (x < max (df. yvar )) else ' orange ' for x in df. yvar ]

#create barplot using specified colors
sns. barplot (x=df. xvar , y=df. yvar , palette=cols)

विधि 3: स्थिति के आधार पर बार का रंग सेट करें

 #use red for bars with value less than 10 and green for all other bars
cols = [' red ' if x < 10 else ' green ' for x in df. yvar ]

#create barplot using specified colors
sns. barplot (x=df. xvar , y=df. yvar , palette=cols)

निम्नलिखित उदाहरण दिखाते हैं कि निम्नलिखित पांडा डेटाफ़्रेम के साथ व्यवहार में प्रत्येक विधि का उपयोग कैसे करें:

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' employee ': ['Andy', 'Bert', 'Chad', 'Doug', 'Eric', 'Frank'],
                   ' sales ': [22, 14, 9, 7, 29, 20]})

#view DataFrame
print (df)

  employee sales
0 Andy 22
1 Bert 14
2 Chad 9
3 Doug 7
4 Eric 29
5 Frank 20

उदाहरण 1: सभी बारों के लिए रंग सेट करें

निम्नलिखित कोड दिखाता है कि सीबॉर्न में बारप्लॉट कैसे बनाएं और प्लॉट में सभी बार के लिए “स्टीलब्लू” रंग का उपयोग कैसे करें:

 import seaborn as sns

#create barplot using steelblue as color for each bar
sns. barplot (x= df.employee ,y= df.sales ,color=' steelblue ')

उदाहरण 2: अधिकतम मान के साथ बार रंग सेट करें

निम्नलिखित कोड दिखाता है कि बारप्लॉट में अधिकतम मान वाले बार के लिए नारंगी और अन्य सभी बार के लिए ग्रे का उपयोग कैसे करें:

 import seaborn as sns

#use orange for bar with max value and gray for all other bars
cols = [' gray ' if (x < max (df. sales )) else ' orange ' for x in df. dirty ]

#create barplot with custom colors
sns. barplot (x=df. employee , y=df. sales , palette=cols) 

अधिकतम मूल्य के साथ सीबॉर्न बारप्लॉट सेट बार रंग

उदाहरण 3: अधिकतम मान के साथ बार रंग सेट करें

निम्नलिखित कोड दिखाता है कि बारप्लॉट में अधिकतम मान वाले बार के लिए नारंगी और अन्य सभी बार के लिए ग्रे का उपयोग कैसे करें:

 import seaborn as sns

#use red for bars with value less than 10 and green for all other bars
cols = [' red ' if x < 10 else ' green ' for x in df. dirty ]

#create barplot with custom colors
sns. barplot (x=df. employee , y=df. sales , palette=cols) 

स्थिति के आधार पर रंगों के साथ सीबॉर्न बार प्लॉट

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

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

सीबॉर्न में समूहीकृत बारप्लॉट कैसे बनाएं
सीबॉर्न में पाई चार्ट कैसे बनाएं
एक ही चित्र में एकाधिक सीबॉर्न प्लॉट कैसे बनाएं

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

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