Python 为什么matplotlib轴应该包含的Line2D对象显示为空?
该图打印得很好,但我无法访问Line2D对象 示例代码如下:Python 为什么matplotlib轴应该包含的Line2D对象显示为空?,python,pandas,matplotlib,axes,Python,Pandas,Matplotlib,Axes,该图打印得很好,但我无法访问Line2D对象 示例代码如下: import pandas as pd import numpy as np from matplotlib import pyplot df=pd.DataFrame({"col1":np.random.rand(10), "col2":np.random.rand(10)}) fig=pyplot.figure() ax=fig.add_subplot(1,1,1) ax=df.plot(kind="scatter", x=
import pandas as pd
import numpy as np
from matplotlib import pyplot
df=pd.DataFrame({"col1":np.random.rand(10), "col2":np.random.rand(10)})
fig=pyplot.figure()
ax=fig.add_subplot(1,1,1)
ax=df.plot(kind="scatter", x="col1", y="col2", ax=ax)
ax.lines # the result is an empty list.
fig.show()
另外,
ax.get_lines()
也给出了相同的结果,即没有行。当我用ax.scatter(…)直接绘图时,也会发生同样的情况。我想这就是你要找的:
import pandas as pd
import numpy as np
from matplotlib import pyplot
from matplotlib.lines import Line2D
df=pd.DataFrame({"col1":np.random.rand(10), "col2":np.random.rand(10)})
fig=pyplot.figure()
ax=fig.add_subplot(1,1,1)
ax=df.plot(kind="scatter", x="col1", y="col2", ax=ax)
line = Line2D(df["col1"],df["col2"]) #create the lines with Line2D
ax.add_line(line) #add the lines to fig
fig.show()
这返回了我想这就是您要寻找的:
import pandas as pd
import numpy as np
from matplotlib import pyplot
from matplotlib.lines import Line2D
df=pd.DataFrame({"col1":np.random.rand(10), "col2":np.random.rand(10)})
fig=pyplot.figure()
ax=fig.add_subplot(1,1,1)
ax=df.plot(kind="scatter", x="col1", y="col2", ax=ax)
line = Line2D(df["col1"],df["col2"]) #create the lines with Line2D
ax.add_line(line) #add the lines to fig
fig.show()
这返回在我看来分散不会产生一行。查看
ax.scatter()
返回的内容。如果您没有在图形中绘制任何线条,可能需要告诉您所需的line
。scatter
位于ax.collections
。谢谢@ImportanceOfBeingErnest和@Thomas Kühn。我试图访问散点图中绘制的数据。是的,ax.collections
是我需要的答案。在我看来,scatter
不会产生一条线。查看ax.scatter()
返回的内容。如果您没有在图形中绘制任何线条,可能需要告诉您所需的line
。scatter
位于ax.collections
。谢谢@ImportanceOfBeingErnest和@Thomas Kühn。我试图访问散点图中绘制的数据。是的,ax.collections
是我需要的答案。我不想画线。我正在尝试访问散点图中绘制的数据。其他的评论已经回答了这个问题。我不是在试图勾勒出这条线。我正在尝试访问散点图中绘制的数据。其他评论回答了这个问题。