Practical Matplotlib Coding Challenges
How would you create a simple line plot of y = x^2 for x ranging from -5 to 5?
Answer:
You would use numpy to generate the x values and then plot them. plt.plot(x, y) creates the line plot, and plt.show() displays it. Remember to import matplotlib.pyplot as plt and numpy as np.
Describe how to add a title and labels for the x and y axes to a Matplotlib plot.
Answer:
After creating the plot, use plt.title('My Plot Title') for the title. For axis labels, use plt.xlabel('X-axis Label') and plt.ylabel('Y-axis Label'). These functions are called before plt.show().
Answer:
Call plt.plot() multiple times, once for each line. To differentiate, specify the label argument for each plot, e.g., plt.plot(x, y1, label='Line 1'). Then, call plt.legend() to display the labels.
Answer:
Use plt.savefig('my_plot.png', dpi=300). The first argument is the filename, and dpi (dots per inch) controls the resolution. Common formats include PNG, JPEG, PDF, and SVG.
Answer:
plt.figure() creates a new figure (window) to draw on. plt.subplot(nrows, ncols, index) creates a grid of subplots within the current figure and activates a specific subplot for plotting. This allows arranging multiple plots in a single figure.
How would you create a scatter plot instead of a line plot?
Answer:
Instead of plt.plot(), use plt.scatter(x, y). You can customize marker style, size, and color using arguments like s (size), c (color), and marker.
How can you change the color and line style of a plot?
Answer:
When calling plt.plot(), use the color argument (e.g., color='red' or color='#FF0000') and linestyle argument (e.g., linestyle='--' for dashed, linestyle=':' for dotted). You can also use a format string like plt.plot(x, y, 'r--').
Describe how to add a grid to a Matplotlib plot.
Answer:
Simply call plt.grid(True) after creating your plot. You can also customize the grid lines using arguments like axis ('x', 'y', or 'both'), color, linestyle, and linewidth.
How do you adjust the x and y axis limits of a plot?
Answer:
Use plt.xlim(xmin, xmax) and plt.ylim(ymin, ymax). These functions set the minimum and maximum values displayed on the respective axes, allowing you to zoom in or out on specific data ranges.
Explain how to create a histogram of a dataset.
Answer:
Use plt.hist(data, bins=num_bins). data is the array of values, and bins specifies the number of bins or the bin edges. You can also add edgecolor='black' for better visualization of bin boundaries.
What is the purpose of plt.tight_layout()?
Answer:
plt.tight_layout() automatically adjusts subplot parameters for a tight layout. This helps prevent labels, titles, or legends from overlapping with other subplots or the figure edges, improving readability.
How would you add text annotations to specific points on a plot?
Answer:
Use plt.annotate('Text', xy=(x_point, y_point), xytext=(x_text, y_text), arrowprops=dict(facecolor='black', shrink=0.05)). xy is the point to annotate, xytext is where the text appears, and arrowprops defines the arrow connecting them.