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
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
(A), (C) and (D) all import the pyplot module, which holds the plotting functions such as and .
(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.
Related questions:
2026: 29 May Shift 2
2025: 3 June Shift 2
2025: 30 May Shift 2