Home »
Python
Matplotlib MCQs
There are different libraries present in Python; Matplotlib is one of them. Matplotlib is a popular Python library. It is used for data visualizations. It has different functions and tools that can be used to make charts, plots, and graphs. Matplotlib is most widely used in data science, analytics, scientific research, engineering, finance, and different other domains for data visualization and exploration.
Matplotlib MCQs with Answers and Explanation
Matplotlib MCQs: This section contains multiple-choice questions and answers on Matplotlib. These MCQs are written for beginners as well as advanced, practice these MCQs to enhance and test the knowledge of Matplotlib.
List of Matplotlib MCQs
1. Who created Matplotlib?
- John D. Hunter
- Pearu Peterson
- Robert Kern
- Stéfan van der Walt
Answer: A) John D. Hunter
Explanation:
Matplotlib was created by John D. Hunter. Who was an American neurobiologist and the original author of Matplotlib.
Discuss this question
2. Is Matplotlib open-source and free?
- Yes
- No
Answer: A) Yes
Explanation:
Yes. Matplotlib is open-source and free.
Discuss this question
3. Matplotlib is designed to be as usable as ____.
- SciPy
- MATLAB
- AI
- All of the above
Answer: B) MATLAB
Explanation:
Matplotlib is designed to be as usable as MATLAB, with the ability to use Python.
Discuss this question
4. Matplotlib is a ____ library for the Python programming language.
- data science
- mathematics
- numpy
- plotting
Answer: D) plotting
Explanation:
Matplotlib is a plotting library for the Python programming language.
Discuss this question
5. Matplotlib can be used in which GUI toolkits?
- Tkinter
- wxPython
- Qt
- All of the above
Answer: D) All of the above
Explanation:
The correct answer is all of the above. Matplotlib can be used in GUI toolkits like Tkinter, wxPython, Qt, or GTK.
Discuss this question
6. Which is the correct command to install Matplotlib?
- pip install matplotlib
- pip install matplotlib.pz
- pip install matplotlib.*
- All of the above
Answer: A) pip install matplotlib
Explanation:
If Python and PIP are installed in your system, then you can easily install the matplotlib library by using the below-given command –
pip install matplotlib
Discuss this question
7. Which is the correct import statement to import the matplotlib module?
- import matplotlib from mat
- import matplotlib
- import matplotlib from plotting
- import matplotlib from plots
Answer: B) import matplotlib
Explanation:
The correct import statement to import the matplotlib module is:
import matplotlib
Discuss this question
8. Which method/attribute is used to check the installed version of matplotlib?
- __version__
- version()
- Both A and B
- None of the above
Answer: A) __version__
Explanation:
The __version__ attribute of matplotlib can be used to check the currently installed version of the matplotlib module. Consider the below-given example –
import matplotlib
print(matplotlib.__version__)
Discuss this question
9. Which is the correct import statement to import pyplot?
- import matplotlib.pyplot as plt
- import pyplot from matplotlib
- Both A and B
- None of the above
Answer: A) import matplotlib.pyplot as plt
Explanation:
The pyplot is the most useful utility /submodule of matplotlib and the correct import statement to import pyplot is:
import matplotlib.pyplot as plt
Discuss this question
10. Which function is used to draw points (markers) in a diagram?
- write()
- draw()
- plot()
- paint()
Answer: C) plot()
Explanation:
The plot() function is used to draw points (markers) in a diagram. The syntax of plot() function is –
plot(x, y, scalex, scaley, data, **kwargs)
Discuss this question
11. Which is the correct code statement to draw a plot without a line?
- plt.plot(xpoints, ypoints)
- plt.plot(xpoints, ypoints, 0)
- plt.plot(xpoints, ypoints, False)
- plt.plot(xpoints, ypoints, 'o')
Answer: D) plt.plot(xpoints, ypoints, 'o')
Explanation:
You can draw a plot without lines by specifying the string notation parameter 'o', which means 'rings'. The correct code statement to draw a plot without a line is:
plt.plot(xpoints, ypoints, 'o')
Discuss this question
12. Which argument keyword can be used to emphasize each point with a specified marker in plotting?
- marker_points
- marker
- ring
- types
Answer: B) marker
Explanation:
When you want to highlight each point with a specified marker, you can use the keyword argument marker with the specified value. Consider the below code statement that can be used to highlight the points with rings –
plt.plot(ypoints, marker = 'o')
Here is the list of the values to highlight the point with a special marker: Reference
Marker |
Description |
'o' |
Circle |
'*' |
Star |
'.' |
Point |
',' |
Pixel |
'x' |
X |
'X' |
X (filled) |
'+' |
Plus |
'P' |
Plus (filled) |
's' |
Square |
'D' |
Diamond |
'd' |
Diamond (thin) |
'p' |
Pentagon |
'H' |
Hexagon |
'h' |
Hexagon |
'v' |
Triangle Down |
'^' |
Triangle Up |
'<' |
Triangle Left |
'>' |
Triangle Right |
'1' |
Tri Down |
'2' |
Tri Up |
'3' |
Tri Left |
'4' |
Tri Right |
'|' |
Vline |
'_' |
Hline |
Discuss this question
13. Which argument keyword can be used to change the style of the plotted line?
- line
- lines
- linestyles
- linestyle
Answer: D) linestyle
Explanation:
When you want to change the style of the plotted line, you can use the keyword argument linestyle with the specified value. Consider the below code statement that can be used to change the style of the plotted line –
plt.plot(ypoints, linestyle = 'dotted')
Discuss this question
14. What is the shorter keyword for the keyword argument 'linestyle' to change the style of the plotted line?
- line
- lines
- style
- ls
Answer: D) ls
Explanation:
The shorter keyword for the keyword argument linestyle to change the style of the plotted line is: ls.
plt.plot(ypoints, ls = 'dotted')
Discuss this question
15. Which function is used to set a label for the x-axis in pyplot?
- xlabel()
- set_x()
- x-axis()
- xaxi()
Answer: A) xlabel()
Explanation:
The xlabel() function is used to set a label for the x-axis. Consider the below code statement to set the x-axis.
plt.xlabel("Average Salary")
Discuss this question
16. Which function is used to set a label for the y-axis in the pyplot?
- ylabel()
- set_y()
- y-axis()
- yaxis()
Answer: A) ylabel()
Explanation:
The ylabel() function is used to set a label for the y-axis. Consider the below code statement to set the y-axis.
plt.ylabel("Year")
Discuss this question
17. Which function is used to set a title for the plot?
- desc()
- title()
- head()
- None of the above
Answer: B) title()
Explanation:
The title() function is used to set a title for the plot. Consider the below code statement to set a title for the plot –
plt.title("Employee Details")
Discuss this question
18. Which parameter is used to set the font properties for the plot?
- fontdict
- fonts
- fontfamily
- plotfonts
Answer: A) fontdict
Explanation:
The fontdict parameter is used to set the font properties for the plot. Consider the below syntax of using fontdict parameter –
# define font properties
font1 = {'family':'Arial','color':'Red','size':17}
# Apply font properties to title
plt.title("Employee Data", fontdict = font1)
Discuss this question
19. The 'fontdict' parameter can be used with which function(s)?
- xlabel()
- ylabel()
- title()
- All of the above
Answer: D) All of the above
Explanation:
The fontdict parameter is used with xlabel(), ylabel(), and title() functions to set font properties for the title and labels.
Discuss this question
20. Which parameter is used to define the position/alignment of the plot title in the title() function?
- pos
- align
- loc
- All of the above
Answer: C) loc
Explanation:
The loc parameter is used to define the position/alignment of the plot title in the title() function with the following values –
Where "center" is the default value.
Consider the below code statement –
plt.title("Employee Data", loc = 'left')
Discuss this question
21. Which function is used to add grid lines to the plot?
- gridlines()
- grids()
- grid()
- gridmarks()
Answer: C) grid()
Explanation:
The grid() function is used to add grid lines to the plot. The syntax of the gird() function is –
# import statement
import matplotlib.pyplot as plt
...
...
plt.plot(x, y) # Draw plot
plt.grid() # Add grids
plt.show() # Show plot with grids
Discuss this question
22. Which is/are the parameter(s) that can be used in grid() function to specify the line properties?
- color
- linestyle
- linewidth
- All of the above
Answer: D) All of the above
Explanation:
The all of the above parameters (color, linestyle, and linewidth) can be used in grid() function to set the various line properties.
plt.grid(color = 'Yellow', linestyle = '--', linewidth = 0.9)
Discuss this question
23. Which function is used to draw multiple figures in one plot?
- subplot()
- subplots()
- pyplots()
- subpyplot()
Answer: A) subplot()
Explanation:
The subplot() function is used to draw multiple figures in one plot. This function takes three arguments to specify the layout of the figure.
Discuss this question
24. In the case of multiple figures drawn by using the subplot() function, which function is used to set the title to the entire figure?
- subplot()
- subplots()
- pyplots()
- subpyplot()
Answer: A) subplot()
Explanation:
If you have drawn multiple figures in one plot by using the subplot() function and want to set the title of the entire figure. You can use suptitle() function. The suptitle() function sets the title of the entire figure having multiple figures.
Discuss this question
25. Which function is used to draw a scatter plot?
- scatterplot()
- pyscatter()
- scatters()
- scatter()
Answer: D) scatter()
Explanation:
To draw a scatter plot, you can simply use the scatter() function. This function is used to plot one dot for each observation. It accepts two arrays of the same length for the x and y-axis.
plt.scatter(x, y)
Where x and y can be the NumPy arrays.
Discuss this question
26. Which parameter(s) is/are used to define the color of the points in a scatter plot?
- color
- c
- Both A and B
- None of the above
Answer: C) Both A and B
Explanation:
The color and its shorter c both can be used to define the color of the points in a scatter plot.
plt.scatter(x, y, color = 'Blue')
Where x and y can be the NumPy arrays.
Discuss this question
27. Which function is used to draw bar graphs?
- bar()
- bars()
- barchat()
- barplot()
Answer: A) bar()
Explanation:
To draw a bar graph, you can use the bar() function. Consider the below code statement to draw a bar graph –
name = ["Alvin", "Alex"]
age = [21, 23]
plt.bar(name, age)
Discuss this question
28. Why barh() function is used in plotting?
- To draw a bar graph with a specified height
- To draw a bar graph to print the highest values of a NumPy array
- To draw a horizontal bar graph
- None of the above
Answer: C) To draw a horizontal bar graph
Explanation:
The barh() function is used to draw a horizontal bar graph. The barh() function takes the same arguments as the "bar()" function. Consider the below code statement to draw a horizontal bar graph –
name = ["Alvin", "Alex"]
age = [21, 23]
plt.barh(name, age)
Discuss this question
29. In a horizontal bar graph, which parameter is used to set the height of the bars?
- height
- barheight
- hgt
- bheight
Answer: A) height
Explanation:
In a horizontal bar graph, the height parameter is used to set the height of the bars. Consider the below code statement –
name = ["Alvin", "Alex"]
age = [21, 23]
plt.barh(name, age , height = 0.1)
Discuss this question
30. Which function is used to create histograms in matplotlib?
- histograms()
- histogram()
- histgraph()
- hist()
Answer: D) hist()
Explanation:
In Matplotlib, the hist() function is used to create histograms. A histogram is a graph showing frequency distributions.
Discuss this question
31. How to print a list of styles available in Matplotlib in order to let you tailor your visualization based on your needs?
- print(plt.style)
- print(plt.style.available)
- print(plt.styles)
- print(plt.styles.available)
Answer: B) print(plt.style.available)
Explanation:
To print a list of styles available in Matplotlib in order to let you tailor your visualization based on your needs, you can use plt.style.available. Consider the below print statement –
print(plt.style.available)
The output would be –
['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']
Discuss this question
32. How to activate a style (for example, you want to activate 'fivethirtyeight' style) in Matplotlib?
- plt.style.use('fivethirtyeight')
- plt.style.apply('fivethirtyeight')
- plt.style('fivethirtyeight')
- plt.style.activate('fivethirtyeight')
Answer: A) plt.style.use('fivethirtyeight')
Explanation:
To activate a style in Matplotlib, you can use plt.style.use() function. Consider the below code statement to apply a style named "fivethirtyeight" –
plt.style.use('fivethirtyeight')
Discuss this question
33. Which plot is also known as the 'Whisker plot' in Matplotlib?
- Bar
- Pie
- Histogram
- Box plot
Answer: D) Box plot
Explanation:
The Box plot is also known as the Whisker plot.
Discuss this question
34. Which is used for plotting a horizontal line?
- hline()
- ahline()
- xhline()
- axhline()
Answer: D) axhline()
Explanation:
For plotting a horizontal line, you can simply use the axhline() function.
Syntax:
matplotlib.pyplot.axhline(y, color, xmin, xmax, linestyle)
Discuss this question
35. Which function is used to save a Matplotlib plot as an image file?
- saveimage()
- saveimg()
- savefig()
- exportimgfig()
Answer: C) savefig()
Explanation:
The savefig() function is used to save a Matplotlib plot as an image file.
Discuss this question
36. Which color is the default color of a Matplotlib plot?
- Red
- Green
- Blue
- Black
Answer: C) Blue
Explanation:
Blue is the default color of a Matplotlib plot.
Discuss this question
37. What is referred to as the graphical representation of data?
- Data Visualization
- Data Analysis
- Data Handling
- Data Plotting
Answer: A) Data Visualization
Explanation:
Data Visualization is referred to as a graphical representation of data.
Discuss this question
38. What is the default type of a histogram in Matplotlib?
- Line
- Bar
- Dotted lines
- Dashed lines
Answer: B) Bar
Explanation:
The default type of a histogram is Bar in Matplotlib.
Discuss this question
39. Which function is used to create a box plot in Matplotlib?
- plt.plot(type='box')
- plt.createbox()
- plt.plotbox()
- plt.boxplot()
Answer: D) plt.boxplot()
Explanation:
The plt.boxplot() function is used to create a box plot in Matplotlib.
Discuss this question
40. What is Pyplot is Matplotlib?
- Function
- Collection
- Class
- Module
Answer: D) Module
Explanation:
Pyplot is a Matplotlib module that provides a MATLAB-like interface.
Discuss this question