该图绘制得很好,但是我无法访问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(...)
绘图时,也会发生相同的情况。 最佳答案
我认为这是您要寻找的:
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()
返回
关于python - 为什么应该由matplotlib轴包含的Line2D对象显示为空?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54501084/