Skip to main contentSkip to solution

Which of the following is the correct method to import matplotlib library to plot graphs?

(A) import matplotlib.pyplot

(B) import matplotlib

(C) from matplotlib import pyplot

(D) import matplotlib.pyplot as pd

Solution

✅ Correct Option: 2

(A), (C) and (D) all import the pyplot module, which holds the plotting functions such as plot()plot() and show()show().

(A) import matplotlib.pyplot loads pyplot, and you call it as matplotlib.pyplot.plot().

(C) from matplotlib import pyplot loads pyplot, and you call it as pyplot.plot().

(D) import matplotlib.pyplot as pd is valid, because an alias can be any name. It works even though plt is the usual alias.


(B) import matplotlib only loads the top-level package. It does not load the pyplot submodule, so it is not a correct way to import the plotting interface.

The alias pd is normally used for pandas, so use plt for pyplot in real code to avoid confusion.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question