Bird
Raised Fist0
NumPydata~10 mins

NumPy with Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - NumPy with Matplotlib
Import NumPy and Matplotlib
↓
Create data array with NumPy
↓
Prepare plot with Matplotlib
↓
Plot data (line, scatter, etc.)
↓
Add labels and title
↓
Show plot on screen
This flow shows how we create data with NumPy and then use Matplotlib to draw and display a graph.
Execution Sample
NumPy
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 5)
y = x ** 2
plt.plot(x, y)
plt.show()
This code creates 5 points from 0 to 10, squares them, and plots the curve.
Execution Table
StepActionVariable/FunctionValue/ResultExplanation
1Import numpynp<module 'numpy'>Load NumPy library as np
2Import matplotlib.pyplotplt<module 'matplotlib.pyplot'>Load plotting library as plt
3Create x arrayx[ 0. 2.5 5. 7.5 10. ]5 points evenly spaced from 0 to 10
4Calculate y = x^2y[ 0. 6.25 25. 56.25 100. ]Square each x value
5Plot x vs yplt.plot(x, y)Line plot createdDraw line connecting points
6Show plotplt.show()Graph window opensDisplay the plot visually
💡 Plot displayed and program waits for user to close window
Variable Tracker
VariableStartAfter Step 3After Step 4Final
xundefined[ 0. 2.5 5. 7.5 10. ][ 0. 2.5 5. 7.5 10. ][ 0. 2.5 5. 7.5 10. ]
yundefinedundefined[ 0. 6.25 25. 56.25 100. ][ 0. 6.25 25. 56.25 100. ]
Key Moments - 3 Insights
Why do we use np.linspace instead of a simple list for x?
np.linspace creates evenly spaced numbers automatically, which is easier and less error-prone than typing a list manually. See execution_table step 3 where x is created.
What does plt.show() do and why is it important?
plt.show() opens the window to display the plot. Without it, the plot might not appear. See execution_table step 6.
Why do we square x using y = x ** 2 instead of a loop?
NumPy allows element-wise operations on arrays directly, making code shorter and faster. This is shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of x after step 3?
A[ 0 1 2 3 4 ]
B[ 0. 2.5 5. 7.5 10. ]
C[ 1 2 3 4 5 ]
D[ 0 2 4 6 8 ]
💡 Hint
Check the 'Value/Result' column at step 3 in the execution_table.
At which step does the program create the y values by squaring x?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the action 'Calculate y = x^2' in the execution_table.
If we change np.linspace(0, 10, 5) to np.linspace(0, 10, 10), what happens to x?
Ax will have 5 points
Bx will be empty
Cx will have 10 points
Dx will have 0 to 5 points
💡 Hint
np.linspace(start, stop, num) creates 'num' points; see variable_tracker for x values.
Concept Snapshot
NumPy with Matplotlib quick steps:
1. Import numpy as np and matplotlib.pyplot as plt
2. Create data arrays with np.linspace or other functions
3. Use plt.plot(x, y) to draw graphs
4. Add labels or titles if needed
5. Call plt.show() to display the plot
Full Transcript
This lesson shows how to use NumPy and Matplotlib together. First, we import both libraries. Then, we create an array x with 5 points from 0 to 10 using np.linspace. Next, we calculate y by squaring each x value. We plot x and y using plt.plot, which draws a line graph. Finally, plt.show() displays the graph window. Variables x and y change as we create and calculate data. Key points include using np.linspace for easy data creation, vectorized operations for calculations, and plt.show() to see the plot.

Practice

(1/5)
1. What is the main purpose of using NumPy together with Matplotlib in data science?
easy
A. To create numerical data and then visualize it with graphs
B. To write text documents and print them
C. To build websites and style them
D. To store large files on the computer

Solution

  1. Step 1: Understand NumPy's role

    NumPy helps create and manage numerical data easily.
  2. Step 2: Understand Matplotlib's role

    Matplotlib is used to draw graphs to visualize data.
  3. Final Answer:

    To create numerical data and then visualize it with graphs -> Option A
  4. Quick Check:

    NumPy + Matplotlib = Data + Graphs [OK]
Hint: NumPy handles numbers, Matplotlib draws pictures [OK]
Common Mistakes:
  • Confusing Matplotlib with text editing tools
  • Thinking NumPy is for website design
  • Mixing file storage with data visualization
2. Which of the following code snippets correctly imports NumPy and Matplotlib for use?
easy
A. import np import matplotlib.pyplot
B. import numpy import matplotlib as plt
C. from numpy import matplotlib import pyplot as plt
D. import numpy as np import matplotlib.pyplot as plt

Solution

  1. Step 1: Check NumPy import syntax

    Correct import is 'import numpy as np' to use 'np' as shortcut.
  2. Step 2: Check Matplotlib import syntax

    Correct import is 'import matplotlib.pyplot as plt' to use 'plt' for plotting.
  3. Final Answer:

    import numpy as np\nimport matplotlib.pyplot as plt -> Option D
  4. Quick Check:

    Standard imports = import numpy as np import matplotlib.pyplot as plt [OK]
Hint: Use 'as np' and 'as plt' for easy code [OK]
Common Mistakes:
  • Using wrong aliases like 'import matplotlib as plt'
  • Forgetting 'pyplot' in Matplotlib import
  • Trying to import non-existing modules
3. What will be the output plot type when running this code?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
medium
A. A line graph showing a sine wave
B. A bar chart with values from 0 to 10
C. A scatter plot with random points
D. No plot will show due to error

Solution

  1. Step 1: Understand data creation

    np.linspace creates 100 points from 0 to 10; y is sine of x values.
  2. Step 2: Understand plotting command

    plt.plot draws a line graph connecting (x, y) points; plt.show displays it.
  3. Final Answer:

    A line graph showing a sine wave -> Option A
  4. Quick Check:

    plot(x, sin(x)) = sine wave line [OK]
Hint: plt.plot with np.sin creates smooth wave line [OK]
Common Mistakes:
  • Confusing plt.plot with bar or scatter plots
  • Expecting random points instead of line
  • Missing plt.show() to display plot
4. Identify the error in this code snippet that tries to plot a cosine wave:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 50)
y = np.cos(x)
plt.plot(x, y)
plt.show
medium
A. np.cos is not a valid NumPy function
B. np.linspace should use 100 points instead of 50
C. Missing parentheses after plt.show to display the plot
D. x and y arrays have different lengths

Solution

  1. Step 1: Check plt.show usage

    plt.show is a function and must be called with parentheses: plt.show()
  2. Step 2: Verify other parts

    np.linspace and np.cos are correct; x and y lengths match.
  3. Final Answer:

    Missing parentheses after plt.show to display the plot -> Option C
  4. Quick Check:

    plt.show() needed to display plot [OK]
Hint: Always call plt.show() with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on plt.show
  • Thinking np.cos is invalid
  • Assuming array length mismatch without checking
5. You want to plot two functions, sine and cosine, on the same graph using NumPy and Matplotlib. Which code correctly does this?
hard
A. import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(np.sin(x)) plt.plot(np.cos(x)) plt.show()
B. import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show()
C. import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x), np.cos(x)) plt.show()
D. import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.scatter(x, np.sin(x)) plt.scatter(x, np.cos(x)) plt.show()

Solution

  1. Step 1: Check plotting both functions

    import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show() plots sine and cosine separately with x values, correct usage.
  2. Step 2: Check legend and show

    import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show() adds legend for clarity and calls plt.show() properly.
  3. Final Answer:

    Code that plots sine and cosine with legend and shows plot -> Option B
  4. Quick Check:

    Separate plt.plot calls + legend + plt.show() = import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 100) plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x)) plt.legend(['sin', 'cos']) plt.show() [OK]
Hint: Plot each function with x, add legend, call plt.show() [OK]
Common Mistakes:
  • Plotting without x values causing wrong x-axis
  • Missing legend to identify lines
  • Using scatter instead of line plot for smooth curves
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard