0% found this document useful (0 votes)
2 views4 pages

Unit3SubPlots Matplotlib DGtutorials - Ipynb-Colab

Uploaded by

deepshree sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Unit3SubPlots Matplotlib DGtutorials - Ipynb-Colab

Uploaded by

deepshree sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

keyboard_arrow_down Unit 3 Sub Plots _Matplotlib _DG tutorials.

ipynb

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2, figsize=(8,6)) # 2x2 grid of subplots

axs[0,0].plot([1,2,3],[4,5,6])
axs[0,0].set_title("Plot 1")

axs[0,1].plot([1,2,3],[6,5,4])
axs[0,1].set_title("Plot 2")

axs[1,0].plot([1,2,3],[1,2,1])
axs[1,0].set_title("Plot 3")

axs[1,1].plot([1,2,3],[3,3,3])
axs[1,1].set_title("Plot 4")

plt.tight_layout() # Adjust spacing automatically


plt.show()

import matplotlib.pyplot as plt

fig, axs = plt.subplots(3, 3, figsize=(8,6)) # 3x2 grid of subplots

axs[0,0].plot([1,2,3],[4,5,6])
axs[0,0].set_title("Plot 1")

axs[0,1].plot([1,2,3],[6,5,4])
axs[0,1].set_title("Plot 2")

axs[1,0].plot([1,2,3],[1,2,1])
axs[1,0].set_title("Plot 3")

axs[0,2].plot([1,2,3],[6,5,4])
axs[0,2].set_title("Plot DG")

axs[1,1].plot([1,2,3],[3,3,3])
axs[1,1].set_title("Plot 4")

axs[2,0].plot([1,2,3],[5,5,5]) # Placeholder for Plot 5


axs[2,0].set_title("Plot 5")

plt.tight_layout() # Adjust spacing automatically


plt.show()
plt.plot([1,2,3,4], [1,4,2,3], color='green', linestyle='--', marker='o')
plt.show()

#color → 'red', 'green', 'blue', or hex codes #FF0000.

#linestyle → '-', '--', '-.', ':'.

#marker → 'o', 's', '^', '*', etc.

plt.plot([1,2,3], [4,5,6], label='Line A')


plt.plot([1,2,3], [6,5,4], label='Line B')

plt.title("Sample Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.xticks([1,2,3], ['One','Two','Three']) # Custom tick labels


plt.yticks([4,5,6])

plt.legend() # Show line labels


plt.show()
xlabel / ylabel → names of axes

xticks / yticks → set positions and labels of ticks

legend → describes the lines

keyboard_arrow_down Plotting Functions in Pandas

import pandas as pd

data = pd.DataFrame({
'Month': ['Jan','Feb','Mar','Apr'],
'Sales': [200, 300, 250, 400],
'Profit': [50, 70, 60, 90]
})

# Line plot
data.plot(x='Month', y='Sales', kind='line', marker='o', color='blue', title='Monthly Sales')

# Bar plot
data.plot(x='Month', y='Profit', kind='bar', color='orange', title='Monthly Profit')

plt.show()

Pandas DataFrames can plot directly, internally using Matplotlib.

kind → type of plot: 'line', 'bar', 'scatter', 'hist', etc.

Pandas handles the figure and axes automatically.

Topic Key Points Example

Figures & Subplots fig, ax = plt.subplots() Multiple plots in one figure

Spacing plt.tight_layout() Avoid overlapping labels

Colors/Markers/Line color , marker , linestyle Customize lines and points


Ticks & Labels xlabel , ylabel , xticks , yticks Customize axes

Legends plt.legend() Label multiple lines


Title plt.title() Add plot title

Pandas Plotting df.plot(x=..., y=..., kind=...) Quick plotting from DataFrame

You might also like