如何按降序对堆积条形图进行排序?
How can I sort my stacked bar chart in descending order?
我正在尝试创建一个堆叠的水平条形图,它是由以下代码创建的,但是我想按频率降序排列。我试图在 groupby 行中添加 .sort_values() 但它不会影响图形。
此 google 驱动器提供的数据:https://drive.google.com/file/d/1PcfYsyD6T1_EHz3FZvdWN2DewzI64_zq/view?usp=sharing
df = pd.read_csv('data.csv')
Company Location DateTime Details Outcome
0 SpaceX LC-39A, Kennedy Space Center, Florida, USA Fri Aug 07, 2020 05:12 UTC Falcon 9 Block 5 | Starlink V1 L9 & BlackSky Success
1 CASC Site 9401 (SLS-2), Jiuquan Satellite Launch Center, China Thu Aug 06, 2020 04:01 UTC Long March 2D | Gaofen-9 04 & Q-SAT Success
2 SpaceX Pad A, Boca Chica, Texas, USA Tue Aug 04, 2020 23:57 UTC Starship Prototype | 150 Meter Hop Success
3 Roscosmos Site 200/39, Baikonur Cosmodrome, Kazakhstan Thu Jul 30, 2020 21:25 UTC Proton-M/Briz-M | Ekspress-80 & Ekspress-103 Success
4 ULA SLC-41, Cape Canaveral AFS, Florida, USA Thu Jul 30, 2020 11:50 UTC Atlas V 541 | Perseverance Success
#add a success column
values = ['Success']
conditions = list(map(df['Outcome'].str.contains, values))
df['outcome'] = np.select(conditions, values, 'Failure')
s = df.groupby(['Company','outcome']).size().sort_values()
s = s.unstack()
outcome Failure Success
Company
AEB 3.0 NaN
AMBA 4.0 4.0
ASI NaN 9.0
Arianespace 10.0 269.0
s.plot(
kind = 'barh', # Horizontal bars
stacked = True,
color = ['Red', 'Green'],
figsize = [15, 8],
)
plt.savefig('hbar.png')
这是我的输出
- 将每个公司的
'Success'
和 'Failure'
相加,然后按总数排序。
import pandas as pd
df = pd.read_csv('data.csv')
#add a success column
values = ['Success']
conditions = list(map(df['Outcome'].str.contains, values))
df['outcome'] = np.select(conditions, values, 'Failure')
s = df.groupby(['Company','outcome']).size().unstack()
# add the row wise sum for each company
s['total'] = s.sum(axis=1)
# sort
s.sort_values('total', inplace=True)
# plot
ax = s[['Failure', 'Success']].plot(kind='barh', stacked=True, color=['Red', 'Green'], figsize=[15, 8])
# set the xscale for better visibility of data
ax.set_xscale('log') # use 'symlog' if there are negative values
plt.savefig('hbar.png')
- 如果 OP 中的数据 link 消失,则数据的可重复性子集
data = {'Company': ['SpaceX', 'CASC', 'SpaceX', 'Roscosmos', 'ULA', 'CASC', 'Roscosmos', 'CASC', 'SpaceX', 'JAXA', 'Northrop', 'ExPace', 'CASC', 'IAI', 'CASC', 'Rocket Lab', 'CASC', 'SpaceX', 'CASC', 'CASC', 'SpaceX', 'Rocket Lab', 'CASC', 'SpaceX', 'CASC', 'SpaceX', 'CASC', 'Virgin Orbit', 'VKS RF', 'MHI', 'ULA', 'ExPace', 'CASC', 'Roscosmos', 'SpaceX', 'IRGC', 'CASC', 'Roscosmos', 'ULA', 'CASC', 'Arianespace', 'SpaceX', 'VKS RF', 'CASC', 'CASC', 'SpaceX', 'VKS RF', 'CASC', 'Arianespace', 'SpaceX', 'Northrop', 'ULA', 'ISA', 'MHI', 'Arianespace', 'Rocket Lab', 'SpaceX', 'SpaceX', 'Arianespace', 'ExPace', 'CASC', 'CASC', 'SpaceX', 'CASC', 'VKS RF', 'Roscosmos', 'ULA', 'CASC', 'Arianespace', 'SpaceX', 'CASC', 'Blue Origin', 'ISRO', 'VKS RF', 'ExPace', 'ExPace', 'Roscosmos', 'Rocket Lab', 'SpaceX', 'CASC', 'ISRO', 'Arianespace', 'VKS RF', 'CASC', 'ExPace', 'CASC', 'ExPace', 'SpaceX', 'CASC', 'CASC', 'Northrop', 'Exos', 'CASC', 'Rocket Lab', 'Northrop', 'ILS', 'CASC', 'VKS RF', 'Roscosmos', 'CASC', 'MHI', 'CASC', 'CASC', 'CASC', 'ExPace', 'VKS RF', 'ISA', 'ULA', 'Roscosmos', 'Rocket Lab', 'CASC', 'CASC', 'ULA', 'SpaceX', 'Arianespace', 'Roscosmos', 'Roscosmos', 'VKS RF', 'CASC', 'SpaceX', 'i-Space', 'ISRO', 'Roscosmos', 'Roscosmos', 'Arianespace', 'VKS RF', 'Roscosmos', 'Exos', 'Rocket Lab', 'SpaceX', 'CASC', 'Arianespace', 'SpaceX', 'CASC', 'Roscosmos', 'Roscosmos', 'SpaceX', 'CASC', 'ISRO', 'CASC', 'Rocket Lab', 'SpaceX', 'Blue Origin', 'CASC', 'CASC', 'Northrop', 'SpaceX', 'Arianespace', 'Roscosmos', 'ISRO', 'CASC', 'Rocket Lab', 'OneSpace', 'Arianespace', 'ULA', 'Roscosmos', 'CASC', 'Exos', 'SpaceX', 'Arianespace', 'SpaceX', 'VKS RF', 'Arianespace', 'ISA', 'ISRO', 'Blue Origin', 'CASC', 'ULA', 'JAXA', 'ISA', 'SpaceX', 'CASC', 'CASC', 'Roscosmos', 'CASC', 'SpaceX', 'CASC', 'VKS RF', 'Arianespace', 'ISRO', 'Rocket Lab', 'CASC', 'CASC', 'SpaceX', 'Arianespace', 'SpaceX', 'Roscosmos', 'VKS RF', 'ISRO', 'Arianespace', 'CASC', 'CASC', 'Northrop', 'Roscosmos', 'SpaceX', 'ISRO', 'Rocket Lab', 'Arianespace', 'VKS RF', 'CASC', 'MHI', 'CASC', 'Landspace', 'Roscosmos', 'CASC', 'Arianespace', 'ULA', 'CASC', 'Roscosmos', 'CASC', 'SpaceX', 'ExPace', 'Arianespace', 'MHI', 'CASC', 'ISRO', 'ULA', 'SpaceX', 'CASC', 'Exos', 'CASC', 'Arianespace', 'ULA', 'SpaceX', 'CASC', 'CASC', 'SpaceX', 'Arianespace', 'SpaceX', 'Blue Origin', 'Roscosmos', 'CASC', 'CASC', 'SpaceX', 'CASC', 'VKS RF', 'MHI', 'Roscosmos', 'CASC', 'SpaceX', 'CASC', 'SpaceX', 'Northrop', 'CASC', 'SpaceX', 'CASC', 'ULA', 'CASC', 'Blue Origin', 'CASC', 'Eurockot', 'SpaceX', 'VKS RF', 'ULA', 'ISRO', 'CASC', 'Arianespace', 'SpaceX', 'CASC', 'SpaceX', 'CASC', 'Roscosmos', 'ISRO', 'Roscosmos', 'CASC', 'Arianespace', 'SpaceX', 'ULA', 'MHI', 'SpaceX', 'Roscosmos', 'CASC', 'SpaceX', 'JAXA', 'CASC', 'Roscosmos', 'SpaceX', 'Arianespace', 'CASC', 'Rocket Lab', 'ULA', 'CASC', 'JAXA', 'CASC', 'ULA', 'ISRO', 'CASC', 'CASC', 'SpaceX', 'Land Launch', 'CASC', 'CASC', 'SpaceX', 'MHI', 'Roscosmos', 'Blue Origin', 'SpaceX', 'Arianespace', 'CASC', 'CASC'], 'Outcome': ['Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Prelaunch Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success']}
df = pd.DataFrame(data)
我正在尝试创建一个堆叠的水平条形图,它是由以下代码创建的,但是我想按频率降序排列。我试图在 groupby 行中添加 .sort_values() 但它不会影响图形。
此 google 驱动器提供的数据:https://drive.google.com/file/d/1PcfYsyD6T1_EHz3FZvdWN2DewzI64_zq/view?usp=sharing
df = pd.read_csv('data.csv')
Company Location DateTime Details Outcome
0 SpaceX LC-39A, Kennedy Space Center, Florida, USA Fri Aug 07, 2020 05:12 UTC Falcon 9 Block 5 | Starlink V1 L9 & BlackSky Success
1 CASC Site 9401 (SLS-2), Jiuquan Satellite Launch Center, China Thu Aug 06, 2020 04:01 UTC Long March 2D | Gaofen-9 04 & Q-SAT Success
2 SpaceX Pad A, Boca Chica, Texas, USA Tue Aug 04, 2020 23:57 UTC Starship Prototype | 150 Meter Hop Success
3 Roscosmos Site 200/39, Baikonur Cosmodrome, Kazakhstan Thu Jul 30, 2020 21:25 UTC Proton-M/Briz-M | Ekspress-80 & Ekspress-103 Success
4 ULA SLC-41, Cape Canaveral AFS, Florida, USA Thu Jul 30, 2020 11:50 UTC Atlas V 541 | Perseverance Success
#add a success column
values = ['Success']
conditions = list(map(df['Outcome'].str.contains, values))
df['outcome'] = np.select(conditions, values, 'Failure')
s = df.groupby(['Company','outcome']).size().sort_values()
s = s.unstack()
outcome Failure Success
Company
AEB 3.0 NaN
AMBA 4.0 4.0
ASI NaN 9.0
Arianespace 10.0 269.0
s.plot(
kind = 'barh', # Horizontal bars
stacked = True,
color = ['Red', 'Green'],
figsize = [15, 8],
)
plt.savefig('hbar.png')
这是我的输出
- 将每个公司的
'Success'
和'Failure'
相加,然后按总数排序。
import pandas as pd
df = pd.read_csv('data.csv')
#add a success column
values = ['Success']
conditions = list(map(df['Outcome'].str.contains, values))
df['outcome'] = np.select(conditions, values, 'Failure')
s = df.groupby(['Company','outcome']).size().unstack()
# add the row wise sum for each company
s['total'] = s.sum(axis=1)
# sort
s.sort_values('total', inplace=True)
# plot
ax = s[['Failure', 'Success']].plot(kind='barh', stacked=True, color=['Red', 'Green'], figsize=[15, 8])
# set the xscale for better visibility of data
ax.set_xscale('log') # use 'symlog' if there are negative values
plt.savefig('hbar.png')
- 如果 OP 中的数据 link 消失,则数据的可重复性子集
data = {'Company': ['SpaceX', 'CASC', 'SpaceX', 'Roscosmos', 'ULA', 'CASC', 'Roscosmos', 'CASC', 'SpaceX', 'JAXA', 'Northrop', 'ExPace', 'CASC', 'IAI', 'CASC', 'Rocket Lab', 'CASC', 'SpaceX', 'CASC', 'CASC', 'SpaceX', 'Rocket Lab', 'CASC', 'SpaceX', 'CASC', 'SpaceX', 'CASC', 'Virgin Orbit', 'VKS RF', 'MHI', 'ULA', 'ExPace', 'CASC', 'Roscosmos', 'SpaceX', 'IRGC', 'CASC', 'Roscosmos', 'ULA', 'CASC', 'Arianespace', 'SpaceX', 'VKS RF', 'CASC', 'CASC', 'SpaceX', 'VKS RF', 'CASC', 'Arianespace', 'SpaceX', 'Northrop', 'ULA', 'ISA', 'MHI', 'Arianespace', 'Rocket Lab', 'SpaceX', 'SpaceX', 'Arianespace', 'ExPace', 'CASC', 'CASC', 'SpaceX', 'CASC', 'VKS RF', 'Roscosmos', 'ULA', 'CASC', 'Arianespace', 'SpaceX', 'CASC', 'Blue Origin', 'ISRO', 'VKS RF', 'ExPace', 'ExPace', 'Roscosmos', 'Rocket Lab', 'SpaceX', 'CASC', 'ISRO', 'Arianespace', 'VKS RF', 'CASC', 'ExPace', 'CASC', 'ExPace', 'SpaceX', 'CASC', 'CASC', 'Northrop', 'Exos', 'CASC', 'Rocket Lab', 'Northrop', 'ILS', 'CASC', 'VKS RF', 'Roscosmos', 'CASC', 'MHI', 'CASC', 'CASC', 'CASC', 'ExPace', 'VKS RF', 'ISA', 'ULA', 'Roscosmos', 'Rocket Lab', 'CASC', 'CASC', 'ULA', 'SpaceX', 'Arianespace', 'Roscosmos', 'Roscosmos', 'VKS RF', 'CASC', 'SpaceX', 'i-Space', 'ISRO', 'Roscosmos', 'Roscosmos', 'Arianespace', 'VKS RF', 'Roscosmos', 'Exos', 'Rocket Lab', 'SpaceX', 'CASC', 'Arianespace', 'SpaceX', 'CASC', 'Roscosmos', 'Roscosmos', 'SpaceX', 'CASC', 'ISRO', 'CASC', 'Rocket Lab', 'SpaceX', 'Blue Origin', 'CASC', 'CASC', 'Northrop', 'SpaceX', 'Arianespace', 'Roscosmos', 'ISRO', 'CASC', 'Rocket Lab', 'OneSpace', 'Arianespace', 'ULA', 'Roscosmos', 'CASC', 'Exos', 'SpaceX', 'Arianespace', 'SpaceX', 'VKS RF', 'Arianespace', 'ISA', 'ISRO', 'Blue Origin', 'CASC', 'ULA', 'JAXA', 'ISA', 'SpaceX', 'CASC', 'CASC', 'Roscosmos', 'CASC', 'SpaceX', 'CASC', 'VKS RF', 'Arianespace', 'ISRO', 'Rocket Lab', 'CASC', 'CASC', 'SpaceX', 'Arianespace', 'SpaceX', 'Roscosmos', 'VKS RF', 'ISRO', 'Arianespace', 'CASC', 'CASC', 'Northrop', 'Roscosmos', 'SpaceX', 'ISRO', 'Rocket Lab', 'Arianespace', 'VKS RF', 'CASC', 'MHI', 'CASC', 'Landspace', 'Roscosmos', 'CASC', 'Arianespace', 'ULA', 'CASC', 'Roscosmos', 'CASC', 'SpaceX', 'ExPace', 'Arianespace', 'MHI', 'CASC', 'ISRO', 'ULA', 'SpaceX', 'CASC', 'Exos', 'CASC', 'Arianespace', 'ULA', 'SpaceX', 'CASC', 'CASC', 'SpaceX', 'Arianespace', 'SpaceX', 'Blue Origin', 'Roscosmos', 'CASC', 'CASC', 'SpaceX', 'CASC', 'VKS RF', 'MHI', 'Roscosmos', 'CASC', 'SpaceX', 'CASC', 'SpaceX', 'Northrop', 'CASC', 'SpaceX', 'CASC', 'ULA', 'CASC', 'Blue Origin', 'CASC', 'Eurockot', 'SpaceX', 'VKS RF', 'ULA', 'ISRO', 'CASC', 'Arianespace', 'SpaceX', 'CASC', 'SpaceX', 'CASC', 'Roscosmos', 'ISRO', 'Roscosmos', 'CASC', 'Arianespace', 'SpaceX', 'ULA', 'MHI', 'SpaceX', 'Roscosmos', 'CASC', 'SpaceX', 'JAXA', 'CASC', 'Roscosmos', 'SpaceX', 'Arianespace', 'CASC', 'Rocket Lab', 'ULA', 'CASC', 'JAXA', 'CASC', 'ULA', 'ISRO', 'CASC', 'CASC', 'SpaceX', 'Land Launch', 'CASC', 'CASC', 'SpaceX', 'MHI', 'Roscosmos', 'Blue Origin', 'SpaceX', 'Arianespace', 'CASC', 'CASC'], 'Outcome': ['Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Prelaunch Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Partial Failure', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success', 'Success']}
df = pd.DataFrame(data)