简体   繁体   中英

Seaborn draw multiple barplots on same x-axis

I am trying to plot two pandas series

Series A

 Private             11210
 Self-emp-not-inc     1321
 Local-gov            1043
 ?                     963
 State-gov             683
 Self-emp-inc          579
 Federal-gov           472
 Without-pay             7
 Never-worked            3
Name: workclass, dtype: int64
Series B

 Self-emp-not-inc    1321
 Local-gov           1043
 State-gov            683
 Self-emp-inc         579
 Federal-gov          472
 Without-pay            7
 Never-worked           3
Name: workclass, dtype: int64
g = sns.barplot(x=A.index, y=A.values, color='green', ax=faxes[ax_id]) # some subplot
g.set_xticklabels(g.get_xticklabels(), rotation=30)
sns.barplot(x=B.index, y=B.values, color='red', ax=faxes[ax_id])

The first plot draws as expected:

在此处输入图像描述

however, once I draw the second something goes wrong (a couple of bar disappear, labels are incorrect, etc).

在此处输入图像描述

Partially related... how can I use log for y-axis (11K vs 3 hides the low number completely)

You can concatenate A and B joining the index. Rows that appear in one but not in the other will be filled in with NaN or NA and will not be shown in the bar plot.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

A = pd.Series({'Private': 11210,
               'Self-emp-not-inc': 1321,
               'Local-gov': 1043,
               '?': 963,
               'State-gov': 683,
               'Self-emp-inc': 579,
               'Federal-gov': 472,
               'Without-pay': 7,
               'Never-worked': 3}, name='workclass')
B = pd.Series({'Self-emp-not-inc': 1321,
               'Local-gov': 1043,
               'State-gov': 683,
               'Self-emp-inc': 579,
               'Federal-gov': 472,
               'Without-pay': 7,
               'Never-worked': 3}, name='workclass')
df = pd.concat([A.rename('workclass A'), B.rename('workclass B')], axis=1)
ax = df.plot.bar(rot=30, color=['darkgreen', 'crimson'])
plt.tight_layout()
plt.show()

结果图

The concatenated dataframe looks like:

                  workclass A  workclass B
Private                 11210          NaN
Self-emp-not-inc         1321       1321.0
Local-gov                1043       1043.0
?                         963          NaN
State-gov                 683        683.0
Self-emp-inc              579        579.0
Federal-gov               472        472.0
Without-pay                 7          7.0
Never-worked                3          3.0

Note that an integer can't be NaN , so B is automatically converted to a float type.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

A = {'Private':11210,                                                                 
'Self-emp-not-inc':1321,                                                         
'Local-gov':1043,                                                        
'?':963,                                                               
'State-gov':683,                                                                 
'Self-emp-inc':579,                                                              
'Federal-gov':472,                                                               
'Without-pay':7,                                                                 
'Never-worked':3}

B = {'Self-emp-not-inc':1321,                                                        
'Local-gov':1043,                                                               
'State-gov':683,                                                                
'Self-emp-inc':579,                                                             
'Federal-gov':472,                                                              
'Without-pay':7,                                                                
'Never-worked':3}

df = pd.concat([pd.Series(A, name='A'), pd.Series(B, name='B')], axis=1)
sns.barplot(y=df.A.values, x=df.index, color='b', alpha=0.4, label='A')
sns.barplot(y=df.B.values, x=df.index, color='r', alpha=0.4, label='B', bottom=df.A.values)
plt.yscale('log')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM