gpt4 book ai didi

python - 视觉分离 Pandas 中的条形图集群

转载 作者:行者123 更新时间:2023-11-28 16:45:49 25 4
gpt4 key购买 nike

这更像是一种几乎可行的 hack。

#!/usr/bin/env python

from pandas import *
import matplotlib.pyplot as plt
from numpy import zeros

# Create original dataframe
df = DataFrame(np.random.rand(5,4), index=['art','mcf','mesa','perl','gcc'],
columns=['pol1','pol2','pol3','pol4'])
# Estimate average
average = df.mean()
average.name = 'average'

# Append dummy row with zeros and then average
row = DataFrame([dict({p:0.0 for p in df.columns}), ])

df = df.append(row)
df = df.append(average)

print df

df.plot(kind='bar')
plt.show()

并给出:

             pol1      pol2      pol3      pol4
art 0.247309 0.139797 0.673009 0.265708
mcf 0.951582 0.319486 0.447658 0.259821
mesa 0.888686 0.177007 0.845190 0.946728
perl 0.902977 0.863369 0.194451 0.698102
gcc 0.836407 0.700306 0.739659 0.265613
0 0.000000 0.000000 0.000000 0.000000
average 0.765392 0.439993 0.579993 0.487194

enter image description here

它给出了基准和平均值之间的视觉分离。有没有办法去掉x轴的0??


事实证明,DataFrame 不允许我以这种方式拥有多个虚拟行。我的解决方案是改变

row = pd.DataFrame([dict({p:0.0 for p in df.columns}), ])  

进入

row = pd.Series([dict({p:0.0 for p in df.columns}), ]) 
row.name = ""

系列可以用空字符串命名。

最佳答案

仍然很 hacky,但它有效:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Create original dataframe
df = pd.DataFrame(np.random.rand(5,4), index=['art','mcf','mesa','perl','gcc'],
columns=['pol1','pol2','pol3','pol4'])
# Estimate average
average = df.mean()
average.name = 'average'

# Append dummy row with zeros and then average
row = pd.DataFrame([dict({p:0.0 for p in df.columns}), ])

df = df.append(row)
df = df.reindex(np.where(df.index, df.index, ''))
df = df.append(average)
print df

df.plot(kind='bar')
plt.show()

enter image description here

关于python - 视觉分离 Pandas 中的条形图集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13983498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com