Issue
Is it possible to get data from a kdeplot when its input is 2D? I have the following:
import numpy as np
from seaborn import kdeplot
lA = np.randon.normal(1,0.2,1000)
ld = np.randon.normal(1,0.2,1000)
kde = kdeplot(x=lA,y=ld)
If this was just 1D, I could get the information with:
lA = np.randon.normal(1,0.2,1000)
kde = kdeplot(lA)
line = kde.lines[0]
x, y = line.get_data()
but as a the input is 2D (lA, ld)
, it returns an <AxesSubplot:>
object and I don't know how to unpack its information since kde.lines[0]
then returns list index out of range
.
I need to compute the maximum and minimum in each of the plotted contour's axis (separately) as my dispersion for each variable.
Solution
You can get the path drawn in the graph, in this case, from the LineCollection object.
import numpy as np
from seaborn import kdeplot
import random
from matplotlib.collections import LineCollection
lA = np.random.normal(1,0.2,1000)
ld = np.random.normal(1,0.2,1000)
kde = kdeplot(x=lA,y=ld)
data = []
for i in kde.get_children():
if i.__class__.__name__ == 'LineCollection':
data.append(i.get_paths())
kde.get_children()
[<matplotlib.collections.LineCollection at 0x28fb3ec2fd0>,
<matplotlib.collections.LineCollection at 0x28fb3ed5320>,
<matplotlib.collections.LineCollection at 0x28fb3ed55f8>,
<matplotlib.collections.LineCollection at 0x28fb3ed58d0>,
<matplotlib.collections.LineCollection at 0x28fb3ed5ba8>,
<matplotlib.collections.LineCollection at 0x28fb3ed5e80>,
<matplotlib.collections.LineCollection at 0x28fb3ee1198>,
<matplotlib.collections.LineCollection at 0x28fb3ee1470>,
<matplotlib.collections.LineCollection at 0x28fb3ee1748>,
<matplotlib.collections.LineCollection at 0x28fb3ee1a20>,
<matplotlib.spines.Spine at 0x28fb0cd3898>,
<matplotlib.spines.Spine at 0x28fb0cd3978>,
<matplotlib.spines.Spine at 0x28fb0cd3a58>,
<matplotlib.spines.Spine at 0x28fb0cd3b38>,
<matplotlib.axis.XAxis at 0x28fb0cd3828>,
<matplotlib.axis.YAxis at 0x28fb0cd3eb8>,
Text(0.5, 1.0, ''),
Text(0.0, 1.0, ''),
Text(1.0, 1.0, ''),
<matplotlib.patches.Rectangle at 0x28fb3eb9630>]
data[0]
[Path(array([[1.0194036 , 0.43072548],
[1.02780525, 0.42839334],
[1.0362069 , 0.4265304 ],
...,
[1.01100196, 0.43337965],
[1.01752133, 0.43134949],
[1.0194036 , 0.43072548]]), None)]
Answered By - r-beginners
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.