matplotlib 当比较1个变量和多个变量时,如何将factorplot条彼此靠近

ergxz8rk  于 2023-04-21  发布在  其他
关注(0)|答案(2)|浏览(88)

我想做一个图来比较一个变量(Fp1)和其他5个变量。我如何使这些条连接起来?我如何消除它们之间的空间?有办法吗?
dataframe:

raw_data = {'Max_Acc': [90.71, 87.98, 92.62, 78.93, 73.69, 73.66, 72.29,
                     92.62, 94.17, 92.62, 83.81, 79.76, 74.40, 72.38],
        'Stage': ['AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL',
                  'AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL'],
        'Elec': ['Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1',
                 'C4', 'T3', 'Fp1', 'P4', 'Fp2', 'Fz', 'Fz']}

df_m=pd.DataFrame(raw_data, columns = ['Max_Acc', 'Stage', 'Elec'])

生成图的代码:

#Seaborn 
sns.set(style="white")
g = sns.factorplot(x="Stage", y="Mean_Acc", hue='Clf', data=df, size=4, aspect=3, kind="bar",
           legend=False) 

g.set(ylim=(0, 120)) 
g.despine(right=False) 
g.set_xlabels("") 
g.set_ylabels("") 
g.set_yticklabels("") 

mylegend=plt.legend(bbox_to_anchor=(0., 1.1, 1., .102), prop ={'size':10}, loc=10, ncol=8, #left, bottom, width,
            title=r'BEST STAGE AFTER OPTIMIZATION')                                #height, loc='center'
mylegend.get_title().set_fontsize('24') 


ax=g.ax 
def annotateBars(row, ax=ax): 
    for p in ax.patches:
        ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
             ha='center', va='center', fontsize=11, color='gray', rotation=90, xytext=(0, 20),
             textcoords='offset points')  

plot = df_m.apply(annotateBars, ax=ax, axis=1)

剧情:

编辑所以我在阅读完答案后做了一些改进。但是我怎么才能让图例中的颜色与条形图中的颜色相匹配呢?我只有2种颜色,但应该是6种。

编辑:使用相同的代码形式的答案

编辑:
所以我升级了Matplotlib并添加了注解条形图的代码

gupuwyp2

gupuwyp21#

factorplot为hue参数的列中的每个唯一项在条形图子组中保留一个位置。因此,您可以引入一个只有两个不同值的新列。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn.apionly as sns

raw_data = {'Max_Acc': [90.71, 87.98, 92.62, 78.93, 73.69, 73.66, 72.29,
                     92.62, 94.17, 92.62, 83.81, 79.76, 74.40, 72.38],
        'Stage': ['AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL',
                  'AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL'],
        'Elec': ['Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1',
                 'C4', 'T3', 'Fp1', 'P4', 'Fp2', 'Fz', 'Fz']}

df_m=pd.DataFrame(raw_data, columns = ['Max_Acc', 'Stage', 'Elec'])
df_m["hue"] = np.arange(0,len(df_m)) // (len(df_m)//2)

g = sns.factorplot(x="Stage", y="Max_Acc", hue="hue", data=df_m, size=4,
                     aspect=3, kind="bar", legend=False)

plt.show()

然后,您需要使用matplotlib进行大量调整以恢复颜色。
在这一点上,您可以决定无论如何使用matplotlib条形图。

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

raw_data = {'Max_Acc': [90.71, 87.98, 92.62, 78.93, 73.69, 73.66, 72.29,
                     92.62, 94.17, 92.62, 83.81, 79.76, 74.40, 72.38],
        'Stage': ['AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL',
                  'AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL'],
        'Elec': ['Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1', 'Fp1',
                 'C4', 'T3', 'Fp1', 'P4', 'Fp2', 'Fz', 'Fz']}

df_m=pd.DataFrame(raw_data)

uelec, uind = np.unique(df_m["Elec"], return_inverse=1)
cmap = plt.cm.get_cmap("Set1")

fig, ax=plt.subplots()
l = len(df_m)
pos = np.arange(0,l) % (l//2) + (np.arange(0,l)//(l//2)-1)*0.4
ax.bar(pos, df_m["Max_Acc"], width=0.4, align="edge", ec="k", color=cmap(uind)  )

handles=[plt.Rectangle((0,0),1,1, color=cmap(i), ec="k") for i in range(len(uelec))]
ax.legend(handles=handles, labels=list(uelec),
           prop ={'size':10}, loc=9, ncol=8, 
            title=r'BEST STAGE AFTER OPTIMIZATION')

ax.set_xticks(range(l//2))
ax.set_xticklabels(df_m["Stage"][:l//2])
ax.set_ylim(0, 120)
plt.show()

若要从海运调色板获取颜色,可以使用

palette = sns.color_palette()
colors= [ palette[i] for i in uind]

...
ax.bar(..., color=colors)
...

handles=[plt.Rectangle((0,0),1,1, color=palette[i], ec="k") for i in range(len(uelec))]
dgsult0t

dgsult0t2#

只有当您没有为每个组和每个色调设置值时才会出现此问题。您可以使用减淡=False来最小化条之间差距
When you plot with dodge=FalseWhen you plot without dodge=False如果你还想添加条形图的值,你需要根据色调设置高度。否则它会给予一个类似“ValueError:posx和posy应该是有限值”

for p in ax.patches:
    if float(p.get_height()) > 0:
        height = p.get_height()
        ax.text(p.get_x()+p.get_width()/2., height + 0.1,height ,ha="center")
    else:
        height = .00000001

相关问题