0% found this document useful (0 votes)
3 views

matlab.ipynb - Colab

The document provides a tutorial on using Matplotlib for various types of data visualizations in Python, including line plots, scatter plots, bar charts, histograms, pie charts, and more. Each section includes code snippets demonstrating how to create the visualizations, along with customization options such as labels, titles, and colors. Additionally, it covers advanced topics like subplots, box plots, annotations, and 3D plots.

Uploaded by

student09hub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

matlab.ipynb - Colab

The document provides a tutorial on using Matplotlib for various types of data visualizations in Python, including line plots, scatter plots, bar charts, histograms, pie charts, and more. Each section includes code snippets demonstrating how to create the visualizations, along with customization options such as labels, titles, and colors. Additionally, it covers advanced topics like subplots, box plots, annotations, and 3D plots.

Uploaded by

student09hub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3/31/25, 12:06 PM matlab.

ipynb - Colab

1) Install and Import Matplotlib

import matplotlib.pyplot as plt


import numpy as np

2) Basic Line Plot

x = np.linspace(0, 10, 100)


y = np.sin(x)
plt.plot(x, y, label="Sine Wave", color="b", linestyle="--", linewidth=2)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Basic Line Plot")
plt.legend()
plt.grid(True)
plt.show()

 

3) Scatter Plot

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

 

https://colab.research.google.com/drive/1G3OrCnbPv3xll8PsxCA-k0nLehSVGg_F#scrollTo=4rXyr2jp6mrT 1/6
3/31/25, 12:06 PM matlab.ipynb - Colab

4) Bar Chart

categories = ['A', 'B', 'C', 'D']


values = [10, 20, 15, 25]
plt.bar(categories, values, color=['blue', 'green', 'red', 'purple'])
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart")
plt.show()

 

5) Horizontal Bar Chart

plt.barh(categories, values, color='orange')


plt.xlabel("Values")
plt.ylabel("Categories")
plt.title("Horizontal Bar Chart")
plt.show()

 

6) Histogram

data = np.random.randn(1000)
plt.hist(data, bins=30, color='skyblue', edgecolor='black')
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.title("Histogram")
plt.show()

https://colab.research.google.com/drive/1G3OrCnbPv3xll8PsxCA-k0nLehSVGg_F#scrollTo=4rXyr2jp6mrT 2/6
3/31/25, 12:06 PM matlab.ipynb - Colab

 

7) Pie Chart

labels = ['Python', 'Java', 'C++', 'JavaScript']


sizes = [30, 25, 20, 25]
colors = ['gold', 'blue', 'red', 'green']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%',
startangle=140)
plt.title("Pie Chart")
plt.show()

 

8) Multiple Line Plots

x = np.linspace(0, 10, 100)


y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="Sine", color="blue")
plt.plot(x, y2, label="Cosine", color="red")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Multiple Line Plots")
plt.legend()
plt.show()

https://colab.research.google.com/drive/1G3OrCnbPv3xll8PsxCA-k0nLehSVGg_F#scrollTo=4rXyr2jp6mrT 3/6
3/31/25, 12:06 PM matlab.ipynb - Colab

 

9) Subplots

x = np.linspace(0, 10, 100)


y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots(2, 1, figsize=(6, 8))
ax[0].plot(x, y1, color="blue")
ax[0].set_title("Sine Wave")
ax[1].plot(x, y2, color="red")
ax[1].set_title("Cosine Wave")
plt.tight_layout()
plt.show()

 

https://colab.research.google.com/drive/1G3OrCnbPv3xll8PsxCA-k0nLehSVGg_F#scrollTo=4rXyr2jp6mrT 4/6
3/31/25, 12:06 PM matlab.ipynb - Colab

10) Box Plot

data = [np.random.randn(100) for _ in range(4)]


plt.boxplot(data, labels=['A', 'B', 'C', 'D'])
plt.title("Box Plot")
plt.show()

<ipython-input-10-9fe5aa439b79>:2: MatplotlibDeprecationWarning: The 'labels' parameter of boxplot() has been renamed 'tick_labels'
plt.boxplot(data, labels=['A', 'B', 'C', 'D'])

11) Adding Text and Annotations

x = np.linspace(0, 10, 100)


y = np.sin(x)
plt.plot(x, y)
plt.text(5, 0, "Midpoint", fontsize=12, color="red")
plt.annotate("Max Point", xy=(1.57, 1), xytext=(2, 0.5),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.title("Annotations Example")
plt.show()

 

13) 3D Plot

from mpl_toolkits.mplot3d import Axes3D


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
ax.scatter(x, y, z, color='green')

https://colab.research.google.com/drive/1G3OrCnbPv3xll8PsxCA-k0nLehSVGg_F#scrollTo=4rXyr2jp6mrT 5/6
3/31/25, 12:06 PM matlab.ipynb - Colab
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.set_title("3D Scatter Plot")
plt.show()

https://colab.research.google.com/drive/1G3OrCnbPv3xll8PsxCA-k0nLehSVGg_F#scrollTo=4rXyr2jp6mrT 6/6

You might also like