WikiGalaxy

Personalize

Combining Matplotlib and NumPy

Introduction:

Matplotlib and NumPy are powerful libraries in Python for data visualization and numerical computations, respectively. Combining these two allows for efficient data manipulation and visualization, providing deeper insights into datasets.

Plotting a Sine Wave

Overview:

Using NumPy to generate data points for a sine wave and Matplotlib to plot it provides an excellent example of their combined capabilities.


import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('Angle [radians]')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()
      

Explanation:

NumPy's linspace function generates 100 points between 0 and 2Ï€, which are used to compute the sine values. Matplotlib then plots these values, creating a smooth sine wave graph.

Visualizing Random Data

Overview:

Generating random data using NumPy and visualizing it with Matplotlib helps in understanding data distribution and trends.


import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(1000)

plt.hist(data, bins=30, alpha=0.5, color='teal')
plt.title('Histogram of Random Data')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
      

Explanation:

The randn function from NumPy generates 1000 random numbers from a standard normal distribution. Matplotlib's histogram function then visualizes this data, showing its distribution.

Scatter Plot of Random Points

Overview:

Creating scatter plots with random data points can help visualize relationships or distributions within datasets.


import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)
y = np.random.rand(100)

plt.scatter(x, y, color='green', alpha=0.5)
plt.title('Random Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
      

Explanation:

NumPy's rand function generates random values for both x and y coordinates, which are then plotted using Matplotlib's scatter function to create a scatter plot.

Bar Chart of Categorical Data

Overview:

Bar charts are useful for comparing quantities across different categories.


import numpy as np
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C', 'D']
values = np.random.randint(1, 10, size=4)

plt.bar(categories, values, color='red')
plt.title('Bar Chart Example')
plt.xlabel('Category')
plt.ylabel('Value')
plt.grid(True)
plt.show()
      

Explanation:

The randint function from NumPy generates random integer values for each category, which are then visualized using Matplotlib's bar function.

Line Plot with Multiple Lines

Overview:

Creating line plots with multiple lines can help compare different datasets or trends over time.


import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, label='sin(x)', color='pink')
plt.plot(x, y2, label='cos(x)', color='purple')
plt.title('Multiple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.grid(True)
plt.show()
      

Explanation:

Two sets of y-values, sine and cosine, are computed using NumPy and plotted on the same graph using Matplotlib. This allows for easy comparison of the two functions.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025