gpt4 book ai didi

使用 Seaborn 在散点图上绘制线条

转载 作者:行者123 更新时间:2023-12-04 23:17:42 26 4
gpt4 key购买 nike

我的目标是在使用 seaborn 绘制的图上的 y = 0 上绘制一条水平红线:sns.lmplotcol= 分割或 row= .

import numpy as np, seaborn as sns, pandas as pd
np.random.seed(5)

myData = pd.DataFrame({'x' : np.arange(1, 101), 'y': np.random.normal(0, 4, 100),\
'z' : ['a','b']*50, 'w':np.random.poisson(0.15,100)})


sns.lmplot("x", "y", col="z", row="w", data=myData, fit_reg=False)
plt.plot(np.linspace(-20,120,1000), [0]*1000, 'r-')

我们可以看到,只有图数组中的最后一个图用红线标记:

enter image description here

谢谢你的帮助,

编辑:改写了这个问题,以说明我们使用 col= 生成一组图的情况。和/或 row=并且我们希望在每个地块上追踪这条线。

最佳答案

所以这段代码适用于我们使用 col= 的一般情况。 , row= , 和 hue= .

import numpy as np, seaborn as sns, pandas as pd
np.random.seed(5)

myData = pd.DataFrame({'x' : np.arange(1, 101), 'y': np.random.normal(0, 4, 100),\
'z' : ['a','b']*50, 'w':np.random.poisson(0.15,100), 'hueMe':['q','w','e','r','t']*20})

myPlot = sns.FacetGrid(col="z", row='w', hue='hueMe', data=myData, size=5)
myPlot = myPlot.map(plt.scatter, "x", "y").set(xlim=(-20,120) , ylim=(-15,15))
myPlot = myPlot.map_dataframe(plt.plot, [-20,120], [0,0], 'r-').add_legend().set_axis_labels("x", "y")
plt.show()

enter image description here

不知道为什么水平线的颜色是每个单独图上使用的最后一种颜色,但我现在放弃了:)

关于使用 Seaborn 在散点图上绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36190495/

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