简体   繁体   English

plt.plot(), object.plot() 有什么区别

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

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; a.set_xticks(np.arange(1,6))有效;

  • b.set_xticks(np.arange(1,6)) doesn't work. b.set_xticks(np.arange(1,6))不起作用。

What's the difference of these two code?这两个代码有什么区别?

your "list" is a pandas DataFrame object.您的“列表”是一个Pandas DataFrame对象。 When you call Book_of_Death_Count.plot() you are using the function DataFrame.plot() , which returns (in most cases) an Axes object.当您调用Book_of_Death_Count.plot()您正在使用函数DataFrame.plot() ,它返回(在大多数情况下)一个 Axes 对象。 Therefore a is of Type Axes and you can use it to access all the methods of that class.因此aAxes类型,您可以使用它来访问该类的所有方法

When you use plt.plot() , the return values is a list of Line2D objects .当您使用plt.plot() ,返回值是Line2D对象的列表。 If you need to access the Axes object (for instance to modify the ticks), use a = plt.gca() .如果您需要访问 Axes 对象(例如修改刻度),请使用a = plt.gca()

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.假设a是一个熊猫系列:每当你调用my_pandas_series.plot() ,返回值是一个Axes实例,一个代表你整体绘制区域的对象。 This object has a set_xticks method for... well, setting the position of the x ticks in the plot.这个对象有一个set_xticks方法......好吧,设置图中 x 刻度的位置。

On the other hand, calling plt.plot() returns a list of Line2D objects .另一方面,调用plt.plot()返回Line2D objects的列表。 Neither the list nor the Line2D objects inside it contains a set_xticks method.列表和其中的Line2D对象都不包含set_xticks方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM