Python NumPy

Exhibit 25.57 provides an example of drawing a line from (1, 3) to (8, 10), using matplotlib.

Matplotlib — Plotting a Basic Line Chart using NumPy
import matplotlib.pyplot as plt
import numpy as np

# Create arrays for x and y points
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

# Generate the plot
plt.plot(xpoints, ypoints)

# Add labels and title for clarity
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Line from (1, 3) to (8, 10)")

# Display the plot
plt.show()
    Matplotlib - Plotting a line chart    

Exhibit 25.57 Matplotlib — Plotting a basic line chart using NumPy. Jupyter notebook.

NumPy is a Python library designed for efficient handling of multi-dimensional arrays. It is derived from “Numerical Python” and offers a powerful tool for array manipulation and numerical operations.

While Python lists can be used as arrays, they can be relatively slow for large-scale numerical computations. NumPy addresses this by providing the ndarray object, which is optimized for speed and memory efficiency. This, combined with NumPy’s rich set of functions, makes it ideal for data science tasks that require efficient array processing.

As shown in Exhibit 25.57, a NumPy ndarray object, can be generated use the array() function. The code creates two NumPy arrays, xpoints and ypoints, holding the x and y coordinates of the data points. The plt.plot() function then uses these arrays to generate the line plot. Finally, labels and a title are added for better understanding, and plt.show() displays the plot.


Previous     Next

Use the Search Bar to find content on MarketingMind.