当我绘制我的 pandas 系列的密度分布时,我使用
.plot(kind='kde')
是否可以获得该图的输出值?如果是的话该怎么做?我需要绘制的值。
-
.plot(kind='kde') https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html返回一个Axes https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes object.
- The raw values can be accessed by
_x
and _y
method of the matplotlib.lines.Line2D
object in the plot.
-
ax.get_children() https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.get_children.html可以检查验证matplotlib.lines.Line2D https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html is at
[0]
.
- 从技术上来说,
._y
and ._x
是“私有”方法,这在对象名称前的单下划线和双下划线的含义是什么? https://stackoverflow.com/q/1301346/7758804
- 测试于
python 3.12.0
, pandas 2.1.1
, matplotlib 3.8.0
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
In [266]:
np.random.seed(2023) # for reproducibility
ser = pd.Series(np.random.randn(1000)) # or df = pd.DataFrame(np.random.randn(1000))
ax = ser.plot(kind='kde') # or ax = df.plot(kind='kde')
In [265]:
ax.get_children() # Line2D at index 0
Out[265]:
[<matplotlib.lines.Line2D at 0x2b10f8322d0>,
<matplotlib.spines.Spine at 0x2b10f7ff3e0>,
<matplotlib.spines.Spine at 0x2b10f69a300>,
<matplotlib.spines.Spine at 0x2b10db33a40>,
<matplotlib.spines.Spine at 0x2b10f7ff410>,
<matplotlib.axis.XAxis at 0x2b10f7ff530>,
<matplotlib.axis.YAxis at 0x2b10f69a2a0>,
Text(0.5, 1.0, ''),
Text(0.0, 1.0, ''),
Text(1.0, 1.0, ''),
<matplotlib.patches.Rectangle at 0x2b104c29f40>]
In [264]:
# get the values
x = ax.get_children()[0]._x
y = ax.get_children()[0]._y
plt.plot(x, y)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)