what's the difference between plt.plot(), object.plot()

时间秒杀一切 提交于 2021-01-28 18:29:37

问题


I made a list

> Book_of_Death_Count

1.0    49
2.0    73
3.0    97
4.0    27
5.0    61
Name: Book of Death, dtype: int64*

When I type

a = Book_of_Death_Count.plot(),
b = plt.plot(Book_of_Death_Count)

The result of the two is the same, but:

  • a.set_xticks(np.arange(1,6)) works;

  • b.set_xticks(np.arange(1,6)) doesn't work.

What's the difference of these two code?


回答1:


your "list" is a pandas DataFrame object. When you call Book_of_Death_Count.plot() you are using the function DataFrame.plot(), which returns (in most cases) an Axes object. Therefore a is of Type Axes and you can use it to access all the methods of that class.

When you use plt.plot(), the return values is a list of Line2D objects. If you need to access the Axes object (for instance to modify the ticks), use a = plt.gca().




回答2:


Assuming a is a pandas Series: whenever you call my_pandas_series.plot(), the return value is an Axes instance, an object which represents you plot region as a whole. This object has a set_xticks method for... well, setting the position of the x ticks in the plot.

On the other hand, calling plt.plot() returns a list of Line2D objects. Neither the list nor the Line2D objects inside it contains a set_xticks method.



来源:https://stackoverflow.com/questions/60227407/whats-the-difference-between-plt-plot-object-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!