AI/ML/DL

Visualizing Data with Matplotlib: Your Guide to Python Plotting

Matplotlib is a 2D plotting library in Python that is used to create static, animated, and interactive visualizations. It is widely used for creating graphs and visualizing data.

Key Features:

  • Creating various plots: Line plots, scatter plots, bar charts, histograms, etc.
  • Customization: Allows a high degree of customization (titles, axis labels, legends, etc.).
  • Integration with other libraries: Works seamlessly with NumPy and Pandas to plot data.
  • Interactive plotting: Supports interactive visualizations through the use of widgets.

Example:

Basic Plot:

import matplotlib.pyplot as plt
import numpy as np

# Basic Line Plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()

output: https://prnt.sc/JPxRfNkPtzfC

Scatter Plot

# Scatter Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y)
plt.title("Random Scatter Plot")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()

output: https://prnt.sc/Et-D8mxb60-t

Bar Plot

# Bar Plot
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C', 'D']
values = [4, 7, 1, 8]
plt.bar(categories, values)
plt.title("Bar Chart Example")
plt.show()

output: https://prnt.sc/0r_WT5NIAuna

Advance Example

# Subplots and Customization
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 2, figsize=(12, 6))

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

ax[0].plot(x, y1, label="Sine")
ax[1].plot(x, y2, label="Cosine", color='orange')

ax[0].set_title("Sine Wave")
ax[1].set_title("Cosine Wave")

for a in ax:
    a.set_xlabel("x-axis")
    a.set_ylabel("y-axis")
    a.legend()

plt.show()

output: https://prnt.sc/szzuSMrgPGI0

Best used for visualizing data with various kinds of plots and charts.


About author

author image

Amrit panta

Fullstack developer, content creator



Scroll to Top