Seaborn在同一x轴上绘制多个条形图

时间:2020-08-17 15:21:12

标签: python seaborn

我正在尝试绘制两个熊猫系列

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])

第一个情节按预期绘制:

enter image description here

但是,一旦我绘制了第二个,就会出问题(几个小节消失,标签不正确,等等)。

enter image description here

部分相关...如何在y轴上使用对数(11K与3完全隐藏了较低的数字)

2 个答案:

答案 0 :(得分:1)

您可以将A和B连接到索引。出现在一个而不出现在另一行中的行将用const xpAthForthePageMenuItem = (PageToNavigateTo) => { return `(//div[contains(text(),"${PageToNavigateTo}")])[2]"; } const elem = await ${`xpAthForthePageMenuItem('somepage')`}; await elem.click(); NaN填充,并且不会在条形图中显示。

NA

resulting 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()

请注意,整数不能为 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 ,因此B会自动转换为浮点类型。

答案 1 :(得分:0)

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')

enter image description here