Data Science with Python Certification Data Science with Python Matplotlib and Seaborn Visualization 3 — Questions and Answers
Question 1: In Matplotlib, how do you set the figure size when creating a new figure?
- plt.figure(size=(10,6))
- plt.figure(figsize=(10,6)) (Correct answer)
- plt.figure(width=10, height=6)
- plt.figure(dimensions=(10,6))
Correct answer: plt.figure(figsize=(10,6))
The figsize parameter accepts a tuple (width, height) in inches to set the figure dimensions.
Question 2: Which Seaborn function is best suited for visualizing the correlation matrix of a DataFrame?
- sns.pairplot()
- sns.heatmap() (Correct answer)
- sns.clustermap()
- sns.corrplot()
Correct answer: sns.heatmap()
sns.heatmap(df.corr()) displays correlation values as color-coded cells, making it ideal for correlation matrices.
Question 3: What does the 'alpha' parameter control in Matplotlib plots?
- Line thickness
- Color saturation
- Transparency/opacity (Correct answer)
- Marker size
Correct answer: Transparency/opacity
The alpha parameter sets the transparency level from 0 (fully transparent) to 1 (fully opaque).
Question 4: Which Seaborn function creates a bar chart that shows the mean (and confidence interval) of a numeric variable for each category?
- sns.countplot()
- sns.barplot() (Correct answer)
- sns.histplot()
- sns.boxplot()
Correct answer: sns.barplot()
sns.barplot() shows point estimates (default: mean) with confidence intervals as error bars for each category.
Question 5: In Matplotlib, which command adds a legend to the current axes using labels provided in plot() calls?
- plt.show_legend()
- plt.legend() (Correct answer)
- plt.add_legend()
- plt.label()
Correct answer: plt.legend()
plt.legend() automatically creates a legend using labels assigned via the 'label' parameter in plot functions.
Question 6: What is the primary difference between sns.histplot() and sns.kdeplot()?
- histplot uses bars for discrete counts; kdeplot uses a smooth curve for estimated density (Correct answer)
- histplot is for 2D data; kdeplot is for 1D data
- histplot normalizes data; kdeplot does not
- histplot requires a DataFrame; kdeplot accepts arrays only
Correct answer: histplot uses bars for discrete counts; kdeplot uses a smooth curve for estimated density
histplot bins data into bars showing counts or frequencies, while kdeplot fits a smooth kernel density estimate curve.
Question 7: How do you add a title to a Matplotlib subplot using the axes object 'ax'?
- ax.title('My Title')
- ax.set_title('My Title') (Correct answer)
- ax.add_title('My Title')
- ax.heading('My Title')
Correct answer: ax.set_title('My Title')
ax.set_title() sets the title of a specific subplot when using object-oriented Matplotlib syntax.
In Matplotlib, how do you set the figure size when creating a new figure?