为什么,都应该在的Line2D对象由matplotlib轴显示为空来控制?

问题描述 投票:1回答:1

该图绘制得很好,但我不能访问的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="col1", y="col2", ax=ax)
ax.lines # the result is an empty list.
fig.show()

ax.get_lines()给出相同的结果,即没有线。当我直接与ax.scatter(...)绘制同样的事情发生。

python pandas matplotlib axes
1个回答
0
投票

我认为这是你在找什么:

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()

这将返回enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.