我正在使用seaborn来绘制一些大脑区域的遗传性。我想突出显示x轴上基于大脑区域的标签。例如,假设我有两个区域,一个是白质区域,一个是灰质区域。我想用红色突出大脑灰质区域,用蓝色突出白质区域。我该怎么做呢?
下面是我使用的代码:
b = sns.barplot(x="names", y="h2" ,data=df, ax = ax1)
ax1.set_xticklabels(labels= df['names'].values.ravel(),rotation=90,fontsize=5)
ax1.errorbar(x=list(range (0,165)),y=df['h2'], yerr=df['std'], fmt='none', c= 'b')
plt.tight_layout()
plt.title('heritability of regions ')
plt.show()我应该添加什么来做我想做的事情?谢谢
发布于 2020-07-16 01:20:54
您可以向dataframe添加一个新列,并将其用作hue参数。要更改刻度盘标签的颜色,您可以循环遍历它们,并根据灰色/白色列使用set_color。
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df = pd.DataFrame({'names': list('abcdefghij'),
'h2': np.random.randint(10, 100, 10),
'grey/white': np.random.choice(['grey', 'white'], 10)})
ax1 = sns.barplot(x='names', y='h2', hue='grey/white', dodge=False, data=df)
ax1.set_xticklabels(labels=df['names'], rotation=90, fontsize=15)
# ax1.errorbar(x=list(range(0, 165)), y=df['h2'], yerr=df['std'], fmt='none', c='b')
for (greywhite, ticklbl) in zip(df['grey/white'], ax1.xaxis.get_ticklabels()):
ticklbl.set_color('red' if greywhite == 'grey' else 'blue')
plt.title('heritability of regions ')
plt.tight_layout()
plt.show()

https://stackoverflow.com/questions/62920132
复制相似问题