[英]Seaborn FacetGrid PointPlot Label Data Points
鑒於以下內容:
import seaborn as sns
attend = sns.load_dataset("attention")
sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
g = sns.FacetGrid(attend, col="subject", col_wrap=5,
size=1.5, ylim=(0, 10))
ax = g.map(sns.pointplot, "solutions", "score", scale=.7)
我想在每一行標記單個數據點(將值標簽代替點)。 在我通過MatPlotLib創建的另一個圖中,這是完成的,如下所示:
for i, text in enumerate(ind):
a.annotate(str(y[i])[:-2], xy=(ind[i], y[i]),fontsize=6, color=c,
bbox=dict(pad=.9,alpha=1, fc='white',color='none'),va='center', ha='center',weight='bold')
但是,由於沒有定義,我不確定這是如何工作的。
我不知道ind
是什么。 但是,如果目的是與它們的坐標標注的點,你可以使用ax.annotate
被映射到一個函數內部FacetGrid
如下:
import matplotlib.pyplot as plt
import seaborn as sns
attend = sns.load_dataset("attention")
sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
g = sns.FacetGrid(attend, col="subject", col_wrap=5,
size=1.5, ylim=(0, 10))
def f(x,y, **kwargs):
ax = sns.pointplot(x,y,**kwargs)
ax.axhline(5, alpha=0.5, color='grey')
for i in range(len(x)):
ax.annotate(str(y.values[i]), xy=(x.values[i]-1, y.values[i]),fontsize=8,
xytext = (0,10), textcoords="offset points",
color=kwargs.get("color","k"),
bbox=dict(pad=.9,alpha=0.2, fc='limegreen',color='none'),
va='center', ha='center',weight='bold')
g.map(f, "solutions", "score", scale=.7)
plt.show()
可能,您需要在注釋中使用xy=(i, y.values[i])
,具體取決於數據的外觀。
請注意,這也通過將axhline
放在該函數中來回答您之前的問題 。
如果目的是通過注釋替換點,請使用xytext = (0,0)
或完全保留該參數; 然后還保持bbox=dict(pad=.9,alpha=1, fc='w',color='none')
並在函數調用中使用markers=""
:
import matplotlib.pyplot as plt
import seaborn as sns
attend = sns.load_dataset("attention")
sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
g = sns.FacetGrid(attend, col="subject", col_wrap=5,
size=1.5, ylim=(0, 10))
def f(x,y, **kwargs):
ax = sns.pointplot(x,y,**kwargs)
ax.axhline(5, alpha=0.5, color='grey')
for i in range(len(x)):
ax.annotate(str(y.values[i]), xy=(i, y.values[i]),fontsize=8,
color=kwargs.get("color","k"),
bbox=dict(pad=.9,alpha=1, fc='w',color='none'),
va='center', ha='center',weight='bold')
g.map(f, "solutions", "score", scale=.7, markers="")
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.