157. Data Visualization with matplotlib

Here are 10 Python snippets demonstrating data visualization with the matplotlib library, showcasing various plotting techniques. Each snippet is separated by a delimiter.


Snippet 1: Basic Line Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]

plt.plot(x, y)
plt.title("Basic Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Snippet 2: Scatter Plot

import matplotlib.pyplot as plt

x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78]

plt.scatter(x, y, color='red', marker='o')
plt.title("Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Snippet 3: Bar Chart


Snippet 4: Histogram


Snippet 5: Pie Chart


Snippet 6: Multiple Lines in One Plot


Snippet 7: Box Plot


Snippet 8: Area Plot


Snippet 9: Horizontal Bar Chart


Snippet 10: Heatmap (Using Matplotlib's imshow)


Last updated