Master Matplotlib: Quick Start Guide to Plotting in Python
This tutorial introduces Matplotlib, a powerful Python 2D plotting library, covering installation, basic usage, multiple figure handling, subplots, and common chart types such as line, scatter, pie, bar, and histogram with clear code examples and visual illustrations.
Matplotlib is a powerful 2D plotting library for Python that works on various platforms.
Runtime Environment
Python must be installed on your machine. Install Matplotlib via pip: sudo pip3 install matplotlib The examples in this article were tested on:
Apple OS X 10.13
Python 3.6.3
matplotlib 2.1.1
numpy 1.13.3
Introduction
Matplotlib can be used in Python scripts, IPython shells, Jupyter notebooks, web application servers, and GUI toolkits. It can easily generate various professional graphs such as histograms, spectra, bar charts, scatter plots, and more, with extensive customization options.
Getting Started Example
# test.py
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(100, 201)
plt.plot(data)
plt.show()This three‑line script creates a simple linear plot.
The resulting figure looks like this:
Multiple Figures
You can create several figure windows using plt.figure():
# figure.py
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(100, 201)
plt.plot(data)
data2 = np.arange(200, 301)
plt.figure()
plt.plot(data2)
plt.show()This code produces two separate windows, each showing a line plot for a different range.
Multiple Subplots
To display several plots in a single window, use plt.subplot():
# subplot.py
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(100, 201)
plt.subplot(2, 1, 1)
plt.plot(data)
data2 = np.arange(200, 301)
plt.subplot(2, 1, 2)
plt.plot(data2)
plt.show()The first subplot occupies the top half, the second the bottom half of the window.
Common Chart Examples
Line Plot
# plot.py
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 6, 9], '-r')
plt.plot([1, 2, 3], [2, 4, 9], ':g')
plt.show()Scatter Plot
# scatter.py
import matplotlib.pyplot as plt
import numpy as np
N = 20
plt.scatter(np.random.rand(N) * 100, np.random.rand(N) * 100, c='r', s=100, alpha=0.5)
plt.scatter(np.random.rand(N) * 100, np.random.rand(N) * 100, c='g', s=200, alpha=0.5)
plt.scatter(np.random.rand(N) * 100, np.random.rand(N) * 100, c='b', s=300, alpha=0.5)
plt.show()Pie Chart
# pie.py
import matplotlib.pyplot as plt
import numpy as np
labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
data = np.random.rand(7) * 100
plt.pie(data, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.legend()
plt.show()Bar Chart
# bar.py
import matplotlib.pyplot as plt
import numpy as np
N = 7
x = np.arange(N)
data = np.random.randint(0, 100, size=N)
colors = np.random.rand(N * 3).reshape(N, -1)
labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
plt.title("Weekday Data")
plt.bar(x, data, alpha=0.8, color=colors, tick_label=labels)
plt.show()Histogram
# hist.py
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.randint(0, n, n) for n in [3000, 4000, 5000]]
labels = ['3K', '4K', '5K']
bins = [0, 100, 500, 1000, 2000, 3000, 4000, 5000]
plt.hist(data, bins=bins, label=labels)
plt.legend()
plt.show()Conclusion
This article covered the basic usage of Matplotlib and demonstrated how to create several common chart types. For more advanced features, refer to the official API documentation linked in each section.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
