Home>Article>Backend Development> Python plotting library Matplotlib introductory tutorial_python
Matplotlib is a 2D drawing library in Python language. It supports various platforms and is powerful enough to easily draw various professional images. This article is an introductory tutorial on the Python drawing library Matplotlib. Friends who are interested should learn it together
Running environment
Because this is a Python Language software package, so you need to first install the Python language environment on your machine. Regarding this, please search the Internet for how to obtain it yourself.
For information on how to install Matplotlib, please see here: Matplotlib Installing.
The author recommends that you install it through pip. The specific method is as follows:
sudo pip3 install matplotlib
The source code and test data in this article can be obtained here :matplotlib_tutorial
The code examples in this article will use another Python library: NumPy. It is recommended that readers have some familiarity with NumPy first. I have written a basic tutorial on NumPy before, see here: Python Machine Learning Library NumPy Tutorial.
The code of this article was tested in the following environment:
Apple OS X 10.13
Python 3.6.3 matplotlib 2.1. 1
numpy 1.13.3
Introduction
Matplotlib applicable for various environments, including:
Python script
IPython shell Jupyter notebook
Getting Started Code Example
Let’s take a look at the simplest code example first, let us feel what Matplotlib is like:# test.py import matplotlib.pyplot as plt import numpy as np data = np.arange(100, 201) plt.plot(data) plt.show()The main logic of this code is only three lines, but it draws a very intuitive linear graph, as shown below: Contrast this line chart, let’s explain the logic of the three lines of code:
Generate an integer array between [100, 200], its value is: [100, 101, 102, … , 200]
Draw it out. Obviously, the plotted value corresponds to the ordinate (y-axis) in the graph. And matplotlib itself sets the abscissa (x-axis) of the graph for us: [0, 100], because we have exactly 100 values
Display this graphic
python3 test.py
Note 1: In the following tutorials, we will gradually explain how to customize every detail in the picture. For example: axes, graphics, coloring, line styles, etc.
Note 2: If it is not necessary, the screenshot below will remove the border outside the graphic, leaving only the main body of the graphic.
Draw multiple graphs at one time#Sometimes, we may want to draw multiple graphs at one time, for example: comparison of two sets of data, or Different ways of displaying a set of data, etc.
You can create multiple figures through the following method:
Multiple figurescan be simply understood as one
figureis a graphics window.matplotlib.pyplot
There will be a defaultfigure
, and we can also create more throughplt.figure()
. As shown in the following code:
# 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 draws the graphics of two windows, each of which is a line graph of different intervals, as shown below :
Note: In the initial state, these two windows are completely overlapping.
Multiple subplotsIn some cases, we want to display multiple graphics in the same window. At this point multiple subplots can be used. The following is a code example:
# 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()
In this code, except for the
subplotfunction, it is all familiar content.subplot
The first two parameters of the function specify the number of subplots, that is: they divide the current graph in the form of a matrix, and the two integers specify the number of rows and columns of the matrix respectively. And the third parameter refers to the index in the matrix.Therefore, the following line of code refers to: the first subplot in the 2 rows and 1 column subplot.
plt.subplot(2, 1, 1)
下面这行代码指的是:2行1列subplot中的第2个subplot。
plt.subplot(2, 1, 2)
所以这段代码的结果是这个样子:
subplot
函数的参数不仅仅支持上面这种形式,还可以将三个整数(10之内的)合并一个整数。例如:2, 1, 1
可以写成211
,2, 1, 2
可以写成212
。
因此,下面这段代码的结果是一样的:
import matplotlib.pyplot as plt import numpy as np data = np.arange(100, 201) plt.subplot(211) plt.plot(data) data2 = np.arange(200, 301) plt.subplot(212) plt.plot(data2) plt.show()
subplot
函数的详细说明参见这里:matplotlib.pyplot.subplot
常用图形示例
Matplotlib可以生成非常多的图形式样,多到令人惊叹的地步。大家可以在这里:Matplotlib Gallery 感受一下。
本文作为第一次的入门教程,我们先来看看最常用的一些图形的绘制。
线性图
前面的例子中,线性图的横轴的点都是自动生成的,而我们很可能希望主动设置它。另外,线条我们可能也希望对其进行定制。看一下下面这个例子:
# 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()
这段代码可以让我们得到这样的图形:
这段代码说明如下:
plot
函数的第一个数组是横轴的值,第二个数组是纵轴的值,所以它们一个是直线,一个是折线; 最后一个参数是由两个字符构成的,分别是线条的样式和颜色。前者是红色的直线,后者是绿色的点线。关于样式和颜色的说明请参见plot
函数的API Doc:matplotlib.pyplot.plot
散点图
scatter
函数用来绘制散点图。同样,这个函数也需要两组配对的数据指定x和y轴的坐标。下面是一段代码示例:
# 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()
这段代码说明如下:
这幅图包含了三组数据,每组数据都包含了20个随机坐标的位置 参数c
表示点的颜色,s
是点的大小,alpha
是透明度
这段代码绘制的图形如下所示:
scatter
函数的详细说明参见这里:matplotlib.pyplot.scatter
饼状图
pie
函数用来绘制饼状图。饼状图通常用来表达集合中各个部分的百分比。
# 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()
这段代码说明如下:
data
是一组包含7个数据的随机数值 图中的标签通过labels
来指定autopct
指定了数值的精度格式plt.axis('equal')
设置了坐标轴大小一致plt.legend()
指明要绘制图例(见下图的右上角)
这段代码输出的图形如下所示:
pie
函数的详细说明参见这里:matplotlib.pyplot.pie
条形图
bar
函数用来绘制条形图。条形图常常用来描述一组数据的对比情况,例如:一周七天,每天的城市车流量。
下面是一个代码示例:
# bar.py import matplotlib.pyplot as plt import numpy as np N = 7 x = np.arange(N) data = np.random.randint(low=0, high=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()
这段代码说明如下:
这幅图展示了一组包含7个随机数值的结果,每个数值是[0, 100]的随机数 它们的颜色也是通过随机数生成的。np.random.rand(N * 3).reshape(N, -1)
表示先生成21(N x 3)个随机数,然后将它们组装成7行,那么每行就是三个数,这对应了颜色的三个组成部分。如果不理解这行代码,请先学习一下Python 机器学习库 NumPy 教程title
指定了图形的标题,labels
指定了标签,alpha
是透明度
这段代码输出的图形如下所示:
bar
函数的详细说明参见这里:matplotlib.pyplot.bar
直方图
hist
函数用来绘制直方图。直方图看起来是条形图有些类似。但它们的含义是不一样的,直方图描述了数据中某个范围内数据出现的频度。这么说有些抽象,我们通过一个代码示例来描述就好理解了:
# 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()
上面这段代码中,[np.random.randint(0, n, n) for n in [3000, 4000, 5000]]
生成了包含了三个数组的数组,这其中:
第一个数组包含了3000个随机数,这些随机数的范围是 [0, 3000) 第二个数组包含了4000个随机数,这些随机数的范围是 [0, 4000) 第三个数组包含了5000个随机数,这些随机数的范围是 [0, 5000)
bins数组用来指定我们显示的直方图的边界,即:[0, 100) 会有一个数据点,[100, 500)会有一个数据点,以此类推。所以最终结果一共会显示7个数据点。同样的,我们指定了标签和图例。
这段代码的输出如下图所示:
在这幅图中,我们看到,三组数据在3000以下都有数据,并且频度是差不多的。但蓝色条只有3000以下的数据,橙色条只有4000以下的数据。这与我们的随机数组数据刚好吻合。
hist
函数的详细说明参见这里:matplotlib.pyplot.hist
结束语
通过本文,我们已经知道了Matplotlib的大致使用方法和几种最基本的图形的绘制方式。
需要说明的是,由于是入门教程,因此本文中我们只给出了这些函数和图形最基本的使用方法。但实际上,它们的功能远不止这么简单。因此本文中我们贴出了这些函数的API地址以便读者进一步的研究。
相关推荐:
The above is the detailed content of Python plotting library Matplotlib introductory tutorial_python. For more information, please follow other related articles on the PHP Chinese website!