Create simple line and scatter plots using Matplotlib. This involves applying various customization techniques to visualize data effectively. Practicing these hands-on exercises will enhance understanding of Matplotlib's fundamental plotting workflow.First, ensure you have Matplotlib and NumPy imported. We'll use NumPy to generate some sample data for our plots.import matplotlib.pyplot as plt import numpy as npPractice 1: Creating and Customizing a Line PlotImagine we want to visualize the trend of a simple quadratic function, say $y = x^2$, over a specific range.Generate Data: Let's create some $x$ values and calculate the corresponding $y$ values.# Generate x values from -5 to 5 x_line = np.linspace(-5, 5, 100) # Calculate y values (y = x^2) y_line = x_line**2Create the Plot: Use plt.plot() to create the line plot.# Create a Figure and an Axes object fig, ax = plt.subplots() # Plot the data ax.plot(x_line, y_line) # Display the plot plt.show()Running this will show a basic line plot of the parabola.Add Labels and Title: Make the plot informative by adding a title and axis labels.fig, ax = plt.subplots() ax.plot(x_line, y_line) # Add title and labels ax.set_title("Quadratic Function: y = x^2") ax.set_xlabel("X Values") ax.set_ylabel("Y Values (x^2)") plt.show()Customize Appearance: Let's change the line to be red and dashed, and make it slightly thicker.fig, ax = plt.subplots() # Plot with customizations ax.plot(x_line, y_line, color='#f03e3e', linestyle='--', linewidth=2) # Red dashed line ax.set_title("Customized Quadratic Function: y = x^2") ax.set_xlabel("X Values") ax.set_ylabel("Y Values (x^2)") plt.show()Set Axis Limits: Focus the view on the positive x-axis values where $x$ is between 0 and 4.fig, ax = plt.subplots() ax.plot(x_line, y_line, color='#f03e3e', linestyle='--', linewidth=2) ax.set_title("Focused Quadratic Function: y = x^2") ax.set_xlabel("X Values") ax.set_ylabel("Y Values (x^2)") # Set x and y axis limits ax.set_xlim(0, 4) ax.set_ylim(0, 16) # y goes up to 4^2 = 16 plt.show()Here is the final code combining all steps:import matplotlib.pyplot as plt import numpy as np # 1. Generate Data x_line = np.linspace(-5, 5, 100) y_line = x_line**2 # 2. Create Figure and Axes fig, ax = plt.subplots() # 3. Plot with Customizations ax.plot(x_line, y_line, color='#f03e3e', linestyle='--', linewidth=2) # 4. Add Title and Labels ax.set_title("Focused Quadratic Function: y = x^2") ax.set_xlabel("X Values") ax.set_ylabel("Y Values (x^2)") # 5. Set Axis Limits ax.set_xlim(0, 4) ax.set_ylim(0, 16) # 6. Display the plot plt.show()This plot now clearly shows the $y = x^2$ relationship for $x$ between 0 and 4, with informative labels and distinct styling.{"data": [{"x": [0.0, 0.0505050505050505, 0.101010101010101, 0.1515151515151515, 0.202020202020202, 0.2525252525252525, 0.303030303030303, 0.3535353535353535, 0.404040404040404, 0.4545454545454545, 0.505050505050505, 0.5555555555555556, 0.606060606060606, 0.6565656565656566, 0.707070707070707, 0.7575757575757575, 0.8080808080808081, 0.8585858585858586, 0.9090909090909091, 0.9595959595959596, 1.01010101010101, 1.0606060606060606, 1.1111111111111112, 1.1616161616161616, 1.2121212121212122, 1.2626262626262626, 1.313131313131313, 1.3636363636363635, 1.414141414141414, 1.4646464646464645, 1.5151515151515151, 1.5656565656565657, 1.616161616161616, 1.6666666666666665, 1.717171717171717, 1.7676767676767676, 1.818181818181818, 1.8686868686868687, 1.919191919191919, 1.9696969696969697, 2.02020202020202, 2.0707070707070707, 2.121212121212121, 2.1717171717171715, 2.2222222222222223, 2.2727272727272725, 2.323232323232323, 2.3737373737373735, 2.424242424242424, 2.4747474747474745, 2.525252525252525, 2.5757575757575757, 2.626262626262626, 2.6767676767676765, 2.727272727272727, 2.7777777777777777, 2.828282828282828, 2.8787878787878787, 2.929292929292929, 2.9797979797979797, 3.03030303030303, 3.0808080808080807, 3.131313131313131, 3.1818181818181817, 3.232323232323232, 3.2828282828282827, 3.333333333333333, 3.3838383838383837, 3.434343434343434, 3.4848484848484847, 3.535353535353535, 3.5858585858585856, 3.636363636363636, 3.6868686868686866, 3.737373737373737, 3.7878787878787877, 3.838383838383838, 3.8888888888888884, 3.939393939393939, 3.98989898989899], "y": [0.0, 0.002550762677277828, 0.010203050709111314, 0.02295686409549993, 0.0408122028364442, 0.0637690669319436, 0.08182745638200042, 0.1251873711866123, 0.1632288113457798, 0.20637177685949966, 0.2546162677277778, 0.3079622839506172, 0.36640982552801226, 0.4299588924600181, 0.4986094847465781, 0.5723616023876927, 0.6512152453833618, 0.735170413733586, 0.824227107438365, 0.918385326497699, 1.017645070911589, 1.122006340679979, 1.2314691358030864, 1.3460334562807313, 1.4656993021129072, 1.590466673299617, 1.720335569840855, 1.855305991736622, 1.9953779389869186, 2.1405514115917403, 2.2908264095510905, 2.446202932864968, 2.6066809815333724, 2.772260555556305, 2.942941654933764, 3.118724279665751, 3.309608429752265, 3.485594105193307, 3.6766813059888754, 3.8728699921389714, 4.074160163643595, 4.280551820502746, 4.492044962716424, 4.70863959028463, 4.930335703207362, 5.157133301484618, 5.389032385116399, 5.626032954102708, 5.868134998443544, 6.115338528138908, 6.367643543188797, 6.625050043593214, 6.887558029352158, 7.155167490465629, 7.427878426933628, 7.705690838756152, 7.988604725933204, 8.276619088464782, 8.579734926350888, 8.88795223959152, 9.19127102818668, 9.509691292136365, 9.833213031440576, 10.161836246099315, 10.49556093611258, 10.83438710148037, 11.178314742192686, 11.527343858259528, 11.8814744496809, 12.240706516456795, 12.605040058587218, 12.974475076072168, 13.348011568911646, 13.72664953710565, 14.11038900065418, 14.499230000000002, 14.893172000000003, 15.292215999999998, 15.696362, 16.0], "type": "scatter", "mode": "lines", "line": {"color": "#f03e3e", "dash": "dash", "width": 2}}], "layout": {"title": {"text": "Focused Quadratic Function: y = x^2"}, "xaxis": {"title": {"text": "X Values"}, "range": [0, 4]}, "yaxis": {"title": {"text": "Y Values (x^2)"}, "range": [0, 16]}, "margin": {"l": 50, "r": 50, "t": 50, "b": 50}, "width": 600, "height": 400}}Line plot showing $y=x^2$ for $x$ from 0 to 4. The line is red and dashed.Practice 2: Creating and Customizing a Scatter PlotNow, let's create a scatter plot to visualize the relationship between two sets of random, but somewhat correlated, data points.Generate Data: We'll create two arrays, x_scatter and y_scatter. Let's make y_scatter depend slightly on x_scatter with some added randomness.# Set a seed for reproducibility np.random.seed(42) # Generate 50 random x values x_scatter = np.random.rand(50) * 10 # Values between 0 and 10 # Generate y values with some correlation and noise y_scatter = x_scatter + np.random.randn(50) * 2 # y = x + random noiseCreate the Plot: Use plt.scatter() to plot the points.fig, ax = plt.subplots() # Create the scatter plot ax.scatter(x_scatter, y_scatter) plt.show()This displays the raw scatter plot.Add Labels and Title: Add context to the plot.fig, ax = plt.subplots() ax.scatter(x_scatter, y_scatter) # Add title and labels ax.set_title("Relationship between Two Variables") ax.set_xlabel("Independent Variable (X)") ax.set_ylabel("Dependent Variable (Y)") plt.show()Customize Appearance: Let's change the marker color to orange, increase the size, and perhaps make them slightly transparent using the alpha parameter.fig, ax = plt.subplots() # Plot with customizations ax.scatter(x_scatter, y_scatter, color='#fd7e14', s=50, alpha=0.7) # Orange, size 50, slightly transparent ax.set_title("Customized Relationship between Two Variables") ax.set_xlabel("Independent Variable (X)") ax.set_ylabel("Dependent Variable (Y)") plt.show()Here is the final code for the scatter plot:import matplotlib.pyplot as plt import numpy as np # 1. Generate Data np.random.seed(42) # for reproducibility x_scatter = np.random.rand(50) * 10 y_scatter = x_scatter + np.random.randn(50) * 2 # 2. Create Figure and Axes fig, ax = plt.subplots() # 3. Plot with Customizations ax.scatter(x_scatter, y_scatter, color='#fd7e14', s=50, alpha=0.7) # Orange, size 50, alpha 0.7 # 4. Add Title and Labels ax.set_title("Customized Relationship between Two Variables") ax.set_xlabel("Independent Variable (X)") ax.set_ylabel("Dependent Variable (Y)") # 5. Display the plot plt.show()This scatter plot shows the individual data points representing the relationship between X and Y, styled for better visibility.{"data": [{"x": [3.74540119, 9.50714306, 7.31993942, 5.98658484, 1.5601864 , 1.5599452 , 0.58083612, 8.66176146, 6.01115012, 7.08072578, 0.20584494, 9.69909852, 8.32442641, 2.12339111, 1.81824967, 1.8340451 , 3.04242243, 5.24756432, 4.31945019, 2.9122914 , 6.11852895, 1.39493861, 2.31828104, 3.96107771, 5.38816734, 4.19194514, 6.852195 , 2.0445225 , 8.78117436, 0.27387593, 6.7046751 , 4.17303803, 5.58689828, 1.40386939, 1.98101489, 8.00744569, 9.68261577, 3.13424178, 6.92322616, 8.76389152, 8.94606664, 0.85044211, 0.39054783, 1.6983042 , 8.78142503, 5.03824175, 8.14531709, 3.9952696 , 3.77749308, 8.31855028], "y": [2.79061776, 9.2164848 , 6.96083071, 6.86679989, 1.92593621, 4.51606347, -0.95694362, 9.21224476, 4.62000868, 7.71611765, 0.6470936 , 10.10130665, 9.98543953, 3.45802195, 0.91015583, 3.1038277 , 4.13920161, 5.81925381, 4.0301932 , 2.4186261 , 6.62309927, 0.84957978, 3.39317333, 5.67149364, 5.47807383, 5.56318854, 7.6163278 , -1.39149913, 10.01190885, 1.04185661, 7.55846295, 3.92624448, 5.80400546, 1.17114984, 5.19841836, 5.10581546, 9.6305094 , 4.62871334, 8.90043367, 8.92514883, 7.09326066, 0.59694276, 3.0320538 , -0.29522598, 10.69231329, 4.26663917, 7.50616697, 5.0731539 , 3.1775189 , 10.51692111], "mode": "markers", "type": "scatter", "marker": {"color": "#fd7e14", "size": 50, "opacity": 0.7}}], "layout": {"title": {"text": "Customized Relationship between Two Variables"}, "xaxis": {"title": {"text": "Independent Variable (X)"}}, "yaxis": {"title": {"text": "Dependent Variable (Y)"}}, "margin": {"l": 50, "r": 50, "t": 50, "b": 50}, "width": 600, "height": 400}}Scatter plot showing the relationship between two variables. Markers are orange, semi-transparent circles.These exercises cover the core skills from this chapter: creating line and scatter plots, adding essential labels and titles, and customizing visual elements like color, line style, and marker properties. Experiment further by trying different functions for the line plot, adjusting the noise level in the scatter plot data, or exploring other color codes and line styles available in Matplotlib. The more you practice, the more comfortable you'll become with these fundamental tools.