pyplot.scatter(dataframe)与dataframe.plot(kind =' scatter')

时间:2016-11-25 19:56:36

标签: python pandas matplotlib plot

我有几个pandas数据帧。我想在单独的散点图中相互绘制几列,并将它们组合为图中的子图。我想相应地标记每个子图。我在使用子图标签工作时遇到了很多麻烦,直到我发现有两种方法直接从数据框绘制,据我所知;见SOpandasdoc

ax0 = plt.scatter(df.column0, df.column5)
type(ax0): matplotlib.collections.PathCollection

ax1 = df.plot(0,5,kind='scatter')
type(ax1): matplotlib.axes._subplots.AxesSubplot

ax.set_title('title')适用于ax1但不适用于返回的ax0  AttributeError: 'PathCollection' object has no attribute 'set_title'

我不明白为什么存在两种不同的方式。使用PathCollections的第一种方法的目的是什么?第二个是在17.0中添加的;第一个是过时的还是有不同的目的?

2 个答案:

答案 0 :(得分:2)

两者之间的区别在于它们来自不同的库。第一个来自matplotlib,第二个来自熊猫。它们也是这样做的,它创建了一个matplotlib散点图,但matplotlib版本返回一个点集合,而pandas版本返回一个matplotlib子图。这使得matplotlib版本更加通用,因为您可以在另一个图中使用点集合。

答案 1 :(得分:1)

如您所见,pandas函数返回一个axes对象。 PathCollection对象也可以使用“获取当前轴”功能解释为轴对象。例如:

plot = plt.scatter(df.column0, df.column5)
ax0 = plt.gca()
type(ax0)
  

<位于0x10d2cde10的matplotlib.axes._subplots.AxesSubplot>

您可能会看到以下更为标准的方式:

fig = plt.figure()
ax0 = plt.add_subplot()
ax0.scatter(df.column0, df.column5)

此时,欢迎您使用set_title等“设置”命令。

希望这有帮助。