1

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?

jfaccioni
  • 7,099
  • 1
  • 9
  • 25
  • 1
    Does this answer your question? [Difference between matplotlib's plot() and pandas plot()?](https://stackoverflow.com/questions/51374539/difference-between-matplotlibs-plot-and-pandas-plot) – Georgy Feb 14 '20 at 13:44

2 Answers2

0

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

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
0

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.

jfaccioni
  • 7,099
  • 1
  • 9
  • 25