Search code examples
pythonpandasdataframeplot

Difference between df.plot(kind='bar') and df.plot.bar()


I use Python 3.12.1 and pandas 2.2.0.
I find df.plot(kind='bar') and df.plot.bar() work in a similar (or even the same?) way.

  1. Is there any difference between these two?
  2. Which is the preferred way?

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html


Solution

  • You are so close to the final answer. As you mentioned in your code, According to the documentation, what these methods return is matplotlib.axes.Axes or numpy.ndarray.

    Here is what df.plot(kind='bar') returns :

    enter image description here

    And here is what df.plot.bar() returns :

    enter image description here

    So the output is equivalent when you are using them with default argument values, Unless you change the backend in df.plot().