I'm confused about two ways of using matplotlib

Screen Link:
https://app.dataquest.io/m/146/guided-project%3A-visualizing-earnings-based-on-college-majors/2/pandas-scatter-plots

As far as I know, there are two way of using matplotlib, one is the usual way:

fig, ax = plt.subplots()
ax.scatter(x,y)
ax.set_title("title")
[...]

The other appears in the guided project above:

dataframe.plot(x="colum", y="colum", kind="a_kind", figsize=(val1,val2), title="title")

Why there are two different way of using matplotlib, and what are the main differences. Which way is prefered. Kindly explain these for me. Thanks.

1 Like

Hello @DngNguyn,
the second one is the pandas backend method. You can think this as a “matplotlib wrapper”. The advantage is that you can continue with pandas syntax and plotting is much easier when you have many columns. Otherwise you have to slice the dataframe manually and then use matplotlib.

Another advantage is that you can replace matplotlib with another plotting library, such as plotly. For this, you will like to define:

pd.options.plotting.backend = "plotly"

(more info here)

Regarding the first use that you mentioned, you have the advantage to control any part of the figure and axes.
Anyway, you can combine both (this is the way I usually do):

f, (ax1, ax2) = plt.subplots(ncols = 2)
df['column1'].plot(ax=ax1)
df['column2'].plot(ax=ax2)

And then play around with ax1 and ax2 to setup font, titles, legends, and so on.

3 Likes

Completing what @fedepereira said, matplotlib has two paradigms.
One is the functional programming. The other is OOP (object-oriented programming).

The first comes because Matplotlib is a library inspired by a proprietary software called MatLab, which is procedural and functional.
The other is because Python itself is OOP.

Recall the way we practiced Python in Step 1 before we are introduced to OOP and after, you can see the similarities.

I don’t know much about the topic, but I remember reading this one time.

2 Likes