Master Matplotlib: Quick Start Guide to Plotting in Python
This beginner-friendly tutorial walks you through setting up Matplotlib, installing the library, and creating a variety of basic visualizations—including line plots, scatter plots, pie charts, bar charts, and histograms—while also covering multiple figures, subplots, and customization options for each chart type.
Runtime Environment
Matplotlib is a powerful 2D plotting library for Python that works across platforms. Before using it, ensure Python is installed on your machine and install Matplotlib via sudo pip3 install matplotlib. The examples were tested on macOS 10.13 with Python 3.6.3, matplotlib 2.1.1, and numpy 1.13.3.
Introduction
Matplotlib supports many environments such as Python scripts, IPython shells, Jupyter notebooks, web application servers, and GUI toolkits. It can easily generate various chart types like histograms, spectra, bar charts, and scatter plots, and offers extensive customization.
Basic Code 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 of integers from 100 to 200.
Plotting Multiple Figures
You can create several figure windows using plt.figure() and plot different data in each:
# figure.py
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(100, 201)
plt.figure()
plt.plot(data)
data2 = np.arange(200, 301)
plt.figure()
plt.plot(data2)
plt.show()The subplot function allows you to split a single window into a grid of subplots. For example, plt.subplot(2, 1, 1) creates the first subplot in a 2‑row, 1‑column layout.
Common Plot 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()The first array defines x‑values, the second y‑values; the style string sets line style and color.
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.show()Three parameters control point color ( c), size ( s), and transparency ( alpha).
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()The chart shows the proportion of each weekday with percentage labels.
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, 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()Each bar represents a random value for a day of the week, with random colors and 80% opacity.
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()The histogram visualizes the frequency distribution of three random datasets across defined bins.
Conclusion
This guide introduced the basic usage of Matplotlib and demonstrated how to create several common chart types. For deeper customization, refer to the official API documentation linked in each code block.
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.
