2.3.1: Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍
Matplotlib is a Python plotting library that produces publication-quality figures. Matplotlib是一个Python绘图库,用于生成出版物质量的图形。 It can be used both in Python scripts and when using Python’s interactive mode. 它既可以在Python脚本中使用,也可以在使用Python的交互模式时使用。 Matplotlib is a very large library, and getting to know it well takes time. Matplotlib是一个非常大的库,了解它需要时间。 But often we don’t need the full matplotlib library in our programs,and this is where Pyplot comes in handy. 但是我们的程序中通常不需要完整的matplotlib库,这就是Pyplot的用武之地。 Pyplot is a collection of functions that make matplotlib work like Matlab,which you may be familiar with. Pyplot是一组函数,使matplotlib像Matlab一样工作,您可能熟悉这些函数。 Pyplot is especially useful for interactive work,for example, when you’d like to explore a dataset or visually examine your simulation results. Pyplot对于交互式工作尤其有用,例如,当您希望浏览数据集或直观地检查模拟结果时。 We’ll be using Pyplot in all our data visualizations. 我们将在所有数据可视化中使用Pyplot。 Pyplot provides what is sometimes called a state machine interface to matplotlib library. Pyplot为matplotlib库提供了有时称为状态机的接口。 You can loosely think of it as a process where you create figures one at a time,and all commands affect the current figure and the current plot. 您可以粗略地将其视为一个一次创建一个地物的过程,所有命令都会影响当前地物和当前绘图。 We will mostly use NumPy arrays for storing the data that we’d like to plot, but we’ll occasionally use other types of data objects such as built-in lists. 我们将主要使用NumPy数组来存储要绘制的数据,但偶尔也会使用其他类型的数据对象,如内置列表。 As you may have realized, saying matplotlib.pyplot is kind of a mouthful, and it’s a lot to type too. 正如您可能已经意识到的那样,说matplotlib.pyplot有点口齿不清,而且打字也很费劲。 That’s why virtually everyone who uses the library imports it as plt, which is a lot shorter. 这就是为什么几乎所有使用该库的人都将其作为plt导入,而plt要短得多。 So to import the library, we will type the following– import matplotlib.pyplot as plt. 因此,要导入库,我们将键入以下内容–import matplotlib.pyplot as plt。 Now we are ready to start our plotting. 现在我们准备开始我们的阴谋。 A basis but very useful command is the plt plot function, which can be used to plot lines and markers. plt plot函数是一个基本但非常有用的命令,可用于绘制线和标记。 The simplest version of plot has just one argument,and it specifies the y-axis values that are to be plotted. plot的最简单版本只有一个参数,它指定要打印的y轴值。 In this case, when you provide just one argument, the plot function,each y-axis value will be plotted against its corresponding index value on the x-axis. 在这种情况下,当您只提供一个参数(plot函数)时,每个y轴值都将根据其对应的索引值在x轴上进行打印。 Because Python indexing starts at 0, the first element of your array or list ppears at location x equal to 0. 因为Python索引从0开始,所以数组或列表的第一个元素出现在位置x等于0。 The second element appears at location x equal to 1, and so on. 第二个元素出现在等于1的位置x,依此类推。 Let’s now practice using the plot function in the IPython Shell. 现在让我们练习在IPython Shell中使用plot函数。 Let’s start by making a simple plot. 让我们先画一个简单的图。 So we’ll say plt.plot. 所以我们说plt.plot。 I’m going to create a list consisting of the numbers 0, 1, 4, 9, and 16. 我将创建一个由数字0、1、4、9和16组成的列表。 You can see here that Python returns a matplotlib object. 您可以在这里看到Python返回matplotlib对象。 If for some reason you’d like to suppress the printing of that object,in the IPython Shell you can add a semi-colon at the end of the line and that will do the job. 如果出于某种原因希望禁止打印该对象,则可以在IPython Shell中的行末尾添加分号,这样就可以完成此任务。 If we rerun this with the semi-colon at the end,you’ll see that the plot still appears, but we don’t get that matplotlib object printed out. 如果我们在最后使用分号重新运行此操作,您将看到绘图仍然显示,但我们无法打印matplotlib对象。 Because we are working in IPython, the plots will appear inside the Python Shell. 因为我们在IPython中工作,所以绘图将显示在pythonshell中。 Well, let’s see what happens if, instead of using IPython Shell,we used the standard Python Shell. 好吧,让我们看看如果我们使用标准的Python Shell而不是IPython Shell会发生什么。 Let’s first launch the Python Shell. 让我们首先启动pythonshell。 I’m going to go to View Panes and Console. 我要去查看窗格和控制台。 I’ll resize my window a little bit. 我会稍微调整一下我的窗口。 Because we have a new session here, I need to do my import first. 因为这里有一个新会话,所以我需要先导入。 So I’ve just imported my plt. 所以我刚导入了我的plt。 The next step is to create the plot 0, 1, 4, 9, and 16. 下一步是创建绘图0、1、4、9和16。 You’ll notice that it seems like nothing has happened. 你会注意到好像什么都没发生。 In fact, the plot has been created, but because we are not working in a Python Shell, not IPython Shell,we need to issue the command Show for plt to show the plot. 事实上,已经创建了绘图,但是因为我们不是在pythonshell中工作,不是在ipythonshell中工作,所以我们需要为plt发出Show命令来显示绘图。 So let’s try that– plt.show. 让我们试试看,plt.show。 We can also give the plot function two arguments, in which case the first argument specifies the x-coordinates of the points, 我们也可以给plot函数两个参数,在这种情况下,第一个参数指定点的x坐标, and the second argument the y-coordinates of the points. 第二个参数是点的y坐标。 I’m going to create a vector x using np.linspace. 我将使用np.linspace创建一个向量x。 I want that vector to start at 0 and at 10,and I would like to have 20 points in my vector– in my one-dimensional array. 我希望向量从0到10开始,我希望向量中有20个点,在一维数组中。 I’m going to define my y-vector from the x. 我要用x来定义y向量。 I’m going to be taking every element of x and raising them to power 2. 我将取x的每一个元素,并将它们提升到2次方。 Now I have two vectors defined– x and y. 现在我定义了两个向量——x和y。 I can now call the plot function where my first argument is x and my second argument is y. 现在我可以调用plot函数,其中第一个参数是x,第二个参数是y。 In this case, what we have is the familiar shape of a parabola. 在这种情况下,我们得到的是熟悉的抛物线形状。 We can also provide a third argument to the plot function, which is a format string that specifies color, marker, and line type. 我们还可以为plot函数提供第三个参数,这是一个指定颜色、标记和线型的格式字符串。 Letters and symbols of the format string are the same as in Matlab,but even if you’re not familiar with those, 格式字符串的字母和符号与Matlab中的相同,但即使您不熟悉这些, you’ll easily learn them with a little practice. 只要稍加练习,你就能轻松地学会它们。 This is also a good place to introduce what are called keyword arguments. 这也是介绍所谓的关键字参数的好地方。 The idea is completely generic and applies to all Python functions,but with the plt library, it’s almost impossible not to use them. 这个想法是完全通用的,适用于所有Python函数,但是对于plt库,几乎不可能不使用它们。 In short, a keyword argument is an argument which is supplied to the function by explicitly naming each parameter and specifying its value. 简而言之,关键字参数是通过显式命名每个参数并指定其值提供给函数的参数。 Two keyword arguments that I use all the time are linewidth and markersize. 我一直使用的两个关键字参数是linewidth和markersize。 Let’s put these different ideas together in an example. 让我们把这些不同的想法放在一个例子中。 I’m first going to create my vector x. 我首先要创建向量x。 It’s a linear vector starting from 0, going to 10, and consists of 20 points. 它是一个从0开始到10的线性向量,由20个点组成。 I’m going to define y1, which is going to be my x raised to the power of 2. 我将定义y1,它将是我的x,提升到2的幂。 Then I’m going to define a second vector. 然后我将定义第二个向量。 Let’s call that y2, and this is x raised to the power of 1.5. 我们称之为y2,这是x的幂,1.5。 At this point, I have three vectors– x, y1, and y2. 在这一点上,我有三个向量——x,y1和y2。 I can now call the plot function. 我现在可以调用plot函数了。 My first argument is x, my second argument is y1,and then my third argument specifies the appearance of the plot. 我的第一个参数是x,第二个参数是y1,然后第三个参数指定绘图的外观。 In this case, I’m requesting plt to use blue, to use circles,and to use a solid line. 在本例中,我要求plt使用蓝色、圆形和实线。 We can try plotting this out, and we see the outcome. 我们可以试着画出来,我们看到了结果。 We can now add our keyword arguments to this function call. 现在,我们可以将关键字参数添加到此函数调用中。 I can specify linewidth. 我可以指定线宽。 I’m going to set it to 2 in this case. 在这种情况下,我将把它设置为2。 I’m going to also specify markersize, which I’m going to set to 4. 我还将指定markersize,将其设置为4。 And in this case, you’ll see that the linewidth has changed,and the size of the markers has also changed. 在本例中,您将看到线宽已更改,标记的大小也已更改。 If I would prefer larger markers, I could set markersize 12 and, again, the effect is apparent. 如果我更喜欢较大的标记,我可以将markersize设置为12,同样,效果是明显的。 I can do the same thing for y2. 我可以为y2做同样的事情。 I’m going to use this line and build from it. 我将使用这条线并从中构建。 I’m going to be plotting x, and I’m going to be plotting y2 but in this case, I would like to use green squares,so I change b to g, meaning from blue to green. And I change circles to squares,so I change the o to an s. And I will still stick with my solid lines. 我要画x,我要画y2,但在这个例子中,我想用绿色的方块,所以我把b改成g,意思是从蓝色变成绿色。我把圆变成了正方形,所以我把o变成了s。我仍然会坚持我的实线。 In this case, we run the line and we see the green plot. 在本例中,我们运行该行,并看到绿色的绘图。