11-13-2025, 01:50 PM
matplotlib Quick Plotting Guide (Beginner-Friendly)
matplotlib is the main plotting library used in scientific Python.
It lets you make graphs, charts, and visualisations easily.
This guide gives you the essential commands for science and data analysis.
-----------------------------------------------------------------------
1. Importing matplotlib
The standard import:
-----------------------------------------------------------------------
2. Line Graph (Most Common)
-----------------------------------------------------------------------
3. Scatter Plot
Used for data points and experiments.
-----------------------------------------------------------------------
4. Bar Chart
-----------------------------------------------------------------------
5. Histogram (Distribution of Data)
-----------------------------------------------------------------------
6. Adding Gridlines
Place it before plt.show().
-----------------------------------------------------------------------
7. Changing Line Style & Color
Common line styles:
• "-" solid
• "--" dashed
• ":" dotted
Common colors:
• "red"
• "blue"
• "green"
• "black"
-----------------------------------------------------------------------
8. Multiple Lines on One Graph
-----------------------------------------------------------------------
9. Subplots (Multiple Graphs)
2 rows, 1 column of graphs.
-----------------------------------------------------------------------
10. Saving a Graph to File
Save before plt.show().
-----------------------------------------------------------------------
11. Plotting with pandas
If you have a DataFrame:
-----------------------------------------------------------------------
12. Common Mistakes
❌ Forgetting plt.show()
✔ You must show the graph
❌ Using strings as numbers
✔ Ensure x and y are numeric
❌ Mismatched list lengths
✔ x and y must be same length
❌ Confusing scatter() with plot()
✔ plot() = line graph
✔ scatter() = points only
❌ Saving graph after show()
✔ save BEFORE show()
-----------------------------------------------------------------------
Summary
matplotlib lets you easily create:
• line graphs
• scatter plots
• bar charts
• histograms
• multi-plot layouts
• saved images
It is essential for data science, physics, simulations, and research.
matplotlib is the main plotting library used in scientific Python.
It lets you make graphs, charts, and visualisations easily.
This guide gives you the essential commands for science and data analysis.
-----------------------------------------------------------------------
1. Importing matplotlib
The standard import:
Code:
import matplotlib.pyplot as plt-----------------------------------------------------------------------
2. Line Graph (Most Common)
Code:
x = [0, 1, 2, 3, 4]
y = [0, 2, 5, 7, 10]
plt.plot(x, y)
plt.xlabel("Time (s)")
plt.ylabel("Speed (m/s)")
plt.title("Speed-Time Graph")
plt.show()-----------------------------------------------------------------------
3. Scatter Plot
Used for data points and experiments.
Code:
plt.scatter(x, y)
plt.xlabel("Mass (kg)")
plt.ylabel("Force (N)")
plt.title("Force vs Mass")
plt.show()-----------------------------------------------------------------------
4. Bar Chart
Code:
labels = ["A", "B", "C"]
values = [5, 7, 3]
plt.bar(labels, values)
plt.title("Sample Bar Chart")
plt.show()-----------------------------------------------------------------------
5. Histogram (Distribution of Data)
Code:
data = [3, 4, 6, 7, 7, 8, 9, 10, 12]
plt.hist(data, bins=5)
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.title("Histogram Example")
plt.show()-----------------------------------------------------------------------
6. Adding Gridlines
Code:
plt.grid(True)Place it before plt.show().
-----------------------------------------------------------------------
7. Changing Line Style & Color
Code:
plt.plot(x, y, linestyle="--", color="red")Common line styles:
• "-" solid
• "--" dashed
• ":" dotted
Common colors:
• "red"
• "blue"
• "green"
• "black"
-----------------------------------------------------------------------
8. Multiple Lines on One Graph
Code:
plt.plot(x, y1, label="Trial 1")
plt.plot(x, y2, label="Trial 2")
plt.legend()
plt.show()-----------------------------------------------------------------------
9. Subplots (Multiple Graphs)
Code:
fig, axs = plt.subplots(2, 1)
axs[0].plot(x, y)
axs[1].scatter(x, y)
plt.show()2 rows, 1 column of graphs.
-----------------------------------------------------------------------
10. Saving a Graph to File
Code:
plt.savefig("graph.png", dpi=300)Save before plt.show().
-----------------------------------------------------------------------
11. Plotting with pandas
If you have a DataFrame:
Code:
df.plot(x="Time", y="Speed")
plt.show()-----------------------------------------------------------------------
12. Common Mistakes
❌ Forgetting plt.show()
✔ You must show the graph
❌ Using strings as numbers
✔ Ensure x and y are numeric
❌ Mismatched list lengths
✔ x and y must be same length
❌ Confusing scatter() with plot()
✔ plot() = line graph
✔ scatter() = points only
❌ Saving graph after show()
✔ save BEFORE show()
-----------------------------------------------------------------------
Summary
matplotlib lets you easily create:
• line graphs
• scatter plots
• bar charts
• histograms
• multi-plot layouts
• saved images
It is essential for data science, physics, simulations, and research.
