4. Exploring Python Libraries Further

4.1. Introduction

In this section, we will delve deeper into the use of Python libraries, specifically focusing on advanced operations with NumPy and visualizations using Matplotlib.

Working with Functions

We’ll start by defining the domain of a function and calculating its corresponding range, followed by plotting these values.

import numpy as np

# Define the DOMAIN of a FUNCTION
N = 35
X = np.linspace(-5, 5, N)  # -5 lower limit, 5 upper limit, N number of points to generate
# Display the values
print(X)

# Calculate the CO-DOMAIN of a FUNCTION
Y = np.sin(X) / X
# Display the calculated values
print(Y)

Plotting Complex Functions

Let’s plot the calculated values and explore more advanced plotting techniques.

from matplotlib import pyplot as plt

# Plot the values of X and Y with red circles
plt.plot(X, Y, 'ro')
plt.grid(True)

# Plot the values of X and Y with cyan circles
plt.plot(X, Y, 'co')
plt.grid(True)

# Plot the values of X and Y with blue lines
plt.plot(X, Y, 'b-')
plt.grid(True)
display(plt, "plot_area") # Replace with plt.show() if running locally

Exploring and Composing Functions

We can also explore and compose functions using NumPy and Matplotlib.

Z = (np.sin(X)) ** 2
plt.plot(X, Z, '.-')
plt.grid(True)
display(plt, "plot_area") # Replace with plt.show() if running locally

Interactive Code Editor

To experiment with the code interactively, use the provided interactive code blocks below. Run all the code blocks to see the results and explore different functionalities.

Note

use plt.show() instead of display(plt, “plot_area”) if recreating on local machine.

You have attempted of activities on this page