Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Plot Line Graph from NumPy Array
A line graph is a common way to display the relationship between two dependent datasets. Its general purpose is to show change over time. To plot a line graph from the NumPy array, we can use matplotlib which is the oldest and most widely used Python library for plotting. Also, it can be easily integrated with NumPy which makes it easy to create line graphs to represent trends and patterns in the given datasets.
Basic Line Graph from NumPy Array
Here's how to create a simple line graph using NumPy arrays and matplotlib ?
import numpy as np
import matplotlib.pyplot as plt
# Generate data for plotting
axisX = np.linspace(1, 15, 50)
axisY = np.sin(axisX)
# Create the line graph
plt.plot(axisX, axisY)
# Adding title and labels
plt.title('Line Graph from NumPy Array')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Display the graph
plt.show()
The output shows a sine wave plotted as a smooth line graph ?
[Graph displays a sine wave curve from x=1 to x=15]
Multiple Lines in One Graph
You can plot multiple lines on the same graph by calling plot() multiple times ?
import numpy as np
import matplotlib.pyplot as plt
# Data points for line 1
x1 = np.array([1, 2, 3, 4, 5])
y1 = np.array([2, 4, 6, 8, 10])
# Data points for line 2
x2 = np.array([2, 3, 4, 5, 6])
y2 = np.array([1, 3, 5, 7, 9])
# Data points for line 3
x3 = np.array([1, 2, 3, 4, 5])
y3 = np.array([5, 4, 3, 2, 1])
# Plot all lines with labels
plt.plot(x1, y1, label='Line 1')
plt.plot(x2, y2, label='Line 2')
plt.plot(x3, y3, label='Line 3')
# Add legend and labels
plt.legend()
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Line Plot')
# Display the plot
plt.show()
The output displays three different colored lines with a legend ?
[Graph shows three lines: ascending line, another ascending line, and a descending line with legend]
Customizing Line Appearance
You can customize line styles, colors, and markers for better visualization ?
import numpy as np
import matplotlib.pyplot as plt
# Generate data
x = np.linspace(0, 10, 20)
y1 = np.cos(x)
y2 = np.sin(x)
# Plot with different styles
plt.plot(x, y1, 'r-', label='Cosine', linewidth=2)
plt.plot(x, y2, 'b--', label='Sine', marker='o', markersize=4)
# Customize the plot
plt.title('Customized Line Graph')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.legend()
plt.grid(True, alpha=0.3)
# Display the plot
plt.show()
The output shows customized lines with different colors, styles, and markers ?
[Graph displays red solid cosine line and blue dashed sine line with circle markers and grid]
Key Parameters
| Parameter | Description | Example |
|---|---|---|
label |
Legend label for the line | 'Line 1' |
color |
Line color |
'red' or 'r'
|
linestyle |
Line style |
'-', '--', ':'
|
marker |
Point markers |
'o', 's', '^'
|
linewidth |
Thickness of line |
2, 3.5
|
Conclusion
Use plt.plot() with NumPy arrays to create line graphs. The matplotlib library provides extensive customization options for colors, styles, and markers to create professional-looking visualizations.
