Which of the following reasons best explains why Matplotlib is a popular choice for data visualization in Python?
Think about what features a plotting library should have to be flexible and widely used.
Matplotlib is popular because it offers a simple and flexible way to create many types of plots, including static, animated, and interactive ones. It is not the only plotting library, nor does it preprocess data automatically. It requires coding to use.
What will be the output of the following Python code using Matplotlib?
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.title('Simple Line Plot') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()
Look at the function used to create the plot and the labels added.
The code creates a simple line plot connecting points (1,4), (2,5), and (3,6). It adds a title and labels for X and Y axes. plt.show() displays the plot.
Given the following code, what is the output array representing the counts in each bin?
import matplotlib.pyplot as plt import numpy as np data = [1, 2, 2, 3, 4, 5, 5, 5, 6] counts, bins, patches = plt.hist(data, bins=3) plt.close() print(counts)
Bins split the data range into 3 equal parts. Count how many data points fall into each bin.
The data ranges from 1 to 6. Dividing into 3 bins: [1-2.666...), [2.666-4.333...), [4.333-6]. Counts are 3 in first bin (1,2,2), 2 in second bin (3,4), and 4 in third bin (5,5,5,6).
Which code snippet correctly creates a scatter plot where points are colored based on their Y values?
Check how color mapping is applied in scatter plots and how colorbars are added.
Option A uses the 'c' parameter with y values and a colormap 'viridis' to color points based on y. It also adds a colorbar. Other options either misuse color or omit color mapping.
What error will this code raise when executed?
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) axs[0].plot([1, 2, 3], [4, 5, 6]) plt.show()
Check how axs is indexed when subplots create a 2x2 grid.
plt.subplots(2, 2) returns axs as a 2D numpy array of Axes objects. axs[0] returns a 1D numpy array (first row). Calling .plot() on it raises AttributeError: 'numpy.ndarray' object has no attribute 'plot'. Correct access is axs[0,0].plot(...).