mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.6k stars 1.93k forks source link

Different color for each bar #3767

Closed ovan79 closed 1 month ago

ovan79 commented 1 month ago

Hi.i want to set diferent color for each bar

image

image

def ploting_cnt_amt(df,col, lim=2000) : tmp=pd.crosstab(df[col] , df['isFraud'] , normalize='index')*100 tmp=tmp.reset_index() tmp.rename(columns={ 0:'NoFraud' , 1:'Fraud'},inplace=True)

plt.figure(figsize=(16,14))    
plt.suptitle(f'{col} Distributions ', fontsize=24)

plt.subplot(211)
g = sns.countplot( x=col,  data=df, order=list(tmp[col].values))
gt = g.twinx()
gt = sns.pointplot(x=col, y='Fraud', data=tmp, order=list(tmp[col].values),
                   color='black', legend=False, )
gt.set_ylim(0,tmp['Fraud'].max()*1.1)
gt.set_ylabel("%Fraud Transactions", fontsize=16)
g.set_title(f"Most Frequent {col} values and % Fraud Transactions", fontsize=20)
g.set_xlabel(f"{col} Category Names", fontsize=16)
g.set_ylabel("Count", fontsize=17)
g.set_xticklabels(g.get_xticklabels(),rotation=45)
sizes = []
for p in g.patches:
    height = p.get_height()
    sizes.append(height)
    g.text(p.get_x()+p.get_width()/2.,
            height + 3,
            '{:1.2f}%'.format(height/total*100),
            ha="center",fontsize=12) 

g.set_ylim(0,max(sizes)*1.15)

ploting_cnt_amt(df_trans , 'addr1')

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

The code is the same, but for others, the graph comes out differently for each bar, but my graph only come out in the same color.

I tried setting up 'sns.set_palette()' to work this out, but the phenomenon is the same. Instead, if I set up barplot (palette =...) and set the color, the color comes out differently. It's annoying to set this up every time, is there a way for each bar to automatically come out differently when using it in the future?

mwaskom commented 1 month ago

IIRC, you can use hue. If you need more support, this is a better question for stackoverflow.