Data Science with Python Matplotlib and Seaborn Visualization Questions and Answers — Questions and Answers
Question 1: A data analyst has created a line plot using Matplotlib's `plt.plot()` but finds that the plot is not displayed when the script is run. Which of the following functions must be called to render and display the plot?
- plt.render()
- plt.draw()
- plt.show() (Correct answer)
- plt.display()
Correct answer: plt.show()
In Matplotlib, after creating a plot using functions like `plt.plot()`, you must explicitly call `plt.show()` to display the figure. While some environments like Jupyter notebooks might display plots automatically, `plt.show()` is the standard and required function for rendering the plot in most scripts.
Question 2: You are tasked with visualizing the relationship between two continuous variables, 'total_bill' and 'tip', from a dataset. Additionally, you want to see the distribution of each of these variables individually along the axes of the same plot. Which Seaborn function is best suited for this purpose?
- sns.pairplot()
- sns.scatterplot()
- sns.jointplot() (Correct answer)
- sns.boxplot()
Correct answer: sns.jointplot()
`sns.jointplot()` is specifically designed to solve this problem. It creates a scatter plot to show the bivariate relationship between two variables and adds histograms or KDE plots for each variable in the margins, showing their individual distributions.
Question 3: After generating a scatter plot with Matplotlib, you need to add a descriptive title to the plot and label the x-axis and y-axis. Which of the following code snippets correctly accomplishes this?
- plot.title('My Title'); plot.xlabel('X-Axis'); plot.ylabel('Y-Axis')
- plt.set_title('My Title'); plt.set_xlabel('X-Axis'); plt.set_ylabel('Y-Axis')
- plt.figure(title='My Title', xlabel='X-Axis', ylabel='Y-Axis')
- plt.title('My Title'); plt.xlabel('X-Axis'); plt.ylabel('Y-Axis') (Correct answer)
Correct answer: plt.title('My Title'); plt.xlabel('X-Axis'); plt.ylabel('Y-Axis')
The correct functions in Matplotlib's pyplot interface for adding a title, x-label, and y-label are `plt.title()`, `plt.xlabel()`, and `plt.ylabel()`, respectively. Each function takes a string argument to set the corresponding text on the plot.
Question 4: Which of the following statements best describes a primary advantage of using Seaborn over Matplotlib?
- Seaborn provides more low-level control over every individual plot element.
- Seaborn is a high-level interface that creates visually appealing statistical plots with less code. (Correct answer)
- Seaborn is required for creating basic plots like line charts and bar charts.
- Seaborn offers faster rendering performance for simple visualizations.
Correct answer: Seaborn is a high-level interface that creates visually appealing statistical plots with less code.
Seaborn is built on top of Matplotlib and provides a higher-level API. Its main advantages are its ability to create complex and aesthetically pleasing statistical plots with more concise syntax, its beautiful default styles, and its seamless integration with Pandas DataFrames.
Question 5: A data scientist needs to compare four different data distributions side-by-side in a single figure, arranged in a 2x2 grid. Which Matplotlib function is most commonly used to create the figure and the grid of subplots simultaneously?
- plt.grid(2, 2)
- plt.figure(subplots=4)
- plt.subplots(2, 2) (Correct answer)
- plt.add_subplots(4)
Correct answer: plt.subplots(2, 2)
The `plt.subplots(nrows, ncols)` function is the standard and most convenient way to create a figure and a grid of subplots in one call. It returns both the figure object and a NumPy array of axes objects, which can then be used to create individual plots.
Question 6: To ensure a consistent and professional look for all visualizations in a report, you want to apply a specific color scheme to all subsequent Seaborn plots. Which function should be used to set a default color palette, such as 'colorblind'?
- sns.color_palette('colorblind')
- sns.set(palette='colorblind')
- sns.set_palette('colorblind') (Correct answer)
- sns.style(palette='colorblind')
Correct answer: sns.set_palette('colorblind')
The `sns.set_palette()` function is used to set the default color palette for all subsequent plots created with Seaborn. While `sns.color_palette()` returns a list of colors for a palette, `sns.set_palette()` actually applies it as the default.
A data analyst has created a line plot using Matplotlib's `plt.plot()` but finds that the plot is not displayed when the script is run.
Which of the following functions must be called to render and display the plot?