Python数据分析(中英对照)·Customizing Your Plots-自定义绘图

作者 : jamin 本文共4340个字,预计阅读时间需要11分钟 发布时间: 2020-10-18 共1087人阅读

2.3.2: Customizing Your Plots-自定义绘图

There are a few important elements that can be easily added to plots.
有几个重要元素可以轻松添加到绘图中。
For example, we can add a legend with the legend function.
例如,我们可以使用图例功能添加图例。
We can adjust axes with axis, where axis is spelled A-X-I-S.
我们可以用axis调整轴,其中axis拼写为A-X-I-S。
We can set axis labels using xlabel and ylabel.
我们可以使用xlabel和ylabel设置轴标签。
And we can save a figure using savefig.
我们可以使用savefig保存一个图形。
In that case, the file format extension specifies the format of the file,such as pdf or png.
在这种情况下,文件格式扩展名指定文件的格式,如pdf或png。
Let’s now add these elements to our previous plot.
现在,让我们将这些元素添加到上一个绘图中。
I’m going to construct this plot in the editor.
我将在编辑器中构建这个情节。
So I’m going to take my first line and place that in the editor.
所以我要把我的第一行放到编辑器中。
Then I’m going to take my second line and just copy paste that in the editor.
然后,我将获取第二行,并将其复制粘贴到编辑器中。
If I want to construct the full plot, I’m going to find my definition of x, so we have a full example,x was defined here.
如果我想构造完整的图,我会找到我对x的定义,所以我们有一个完整的例子,x在这里被定义。
Then we had definitions of y1, which was given here.
然后我们有了y1的定义,这里给出了。
And we have also our definition of y2, which is here.
我们还有y2的定义,在这里。
This is the plot that we’ve been looking at so far.
这是我们到目前为止一直在看的情节。
I’m going to start by adding axes labels to this plot.
我将首先向这个图中添加轴标签。
I’m going to type plt.xlabel.
我要输入plt.xlabel。
And we’ll just put it in an X for the x-axis.
我们把它放在X轴上。
And we can use the same idea for ylabel, in which case we’ll just call it Y.
我们可以对ylabel使用相同的想法,在这种情况下,我们将其称为Y。
If you’re familiar with LaTeX, which is the typesetting software often used in mathematical publications, you’ll be pleased to know that plt also knows LaTeX.
如果您熟悉LaTeX,这是数学出版物中经常使用的排版软件,您会很高兴知道plt也了解LaTeX。
If you’re not familiar with it, here’s a brief idea.
如果你不熟悉它,这里有一个简单的想法。
We can take a mathematical notation or a symbol like x,and we can put dollar signs around that.
我们可以用一个数学符号或者像x这样的符号,我们可以在它周围加上美元符号。
All this does is that it changes the appearance of x and y in your plot.
所有这一切只是改变了绘图中x和y的外观。
Let’s try running the plot up to this point.
让我们试着将绘图运行到这一点。
If you’re not familiar with LaTeX, you can drop the dollar signs and we can re-run the code to see what changes.
如果您不熟悉LaTeX,可以去掉美元符号,我们可以重新运行代码以查看有什么变化。
Pay close attention to the axes labels.
请密切注意轴标签。
The axes labels are still x and y, but the specific appearance of the font looks a little bit different.
轴标签仍然是x和y,但字体的具体外观看起来有点不同。
Let’s then use axis function to adjust the axes.
然后让我们使用axis函数来调整轴。
So I’m going to type plt.axis parentheses and inside goes a list.
所以我要输入plt.axis括号,里面是一个列表。
The first argument is going to be xmin.
第一个参数是xmin。
The second one is xmax.
第二个是xmax。
The third one is ymin.
第三个是ymin。
And the fourth one is ymax.
第四个是ymax。
xmin specifies where the x-axis starts in my plot.
xmin指定x轴在绘图中的起始位置。
I’m going to use the number minus 0.5.
我要用这个数字减去0.5。
xmax, I’m going to use 10.5 so I’m going to be going a little bit over the length of my x vector.
xmax,我将使用10.5,所以我将稍微超过x向量的长度。
For ymin, I’m going to be using minus 5.
对于ymin,我将使用-5。
And for ymax, I’m going to be using 105.
对于ymax,我将使用105。
Let’s try re-running this and see how it changes in a subtle way the appearance of the plot.
让我们试着重新运行它,看看它是如何以一种微妙的方式改变情节的外观的。
You see that the axes have now been adjusted.
您可以看到轴现在已调整。
Let’s then add a legend to our plot.
然后让我们在情节中添加一个传奇。
There are two steps.
有两个步骤。
The first step is to give labels.
第一步是给标签。
I’m going to say a label equal to, let’s just called this First,you’ll see that the label is going to be a string.
我会说一个标签等于,我们先把它叫做,你会看到标签是一个字符串。
I also want to give a label for my second plot.
我还想给我的第二个绘图贴上标签。
I’m just going to call that Second.
我要打第二个电话。
To show the legend, I have to type plt.legend.
要显示图例,我必须键入plt.legend。
And I can use an additional keyword argument, loc for location,to specify the specific location of the legend.
我可以使用一个额外的关键字参数loc for location来指定图例的具体位置。
In this case I would like to use the upper left corner for my legend,and we can try running the code,and we’ll see that the legend appears in the upper left corner.
在本例中,我希望使用左上角作为图例,我们可以尝试运行代码,我们将看到图例显示在左上角。
Finally, we’d like to be able to save our figure to file.
最后,我们希望能够将我们的数字保存到文件中。
To do that, we’ll use the savefig function,and inside the parentheses goes the name of my plot.
为此,我们将使用savefig函数,括号内是我的绘图名称。
I’m just going to call that myplot.
我只想把这叫做我的阴谋。
I’d like this to be a pdf plot so I’ll just enter the pdf file extension here.
我希望这是一个pdf绘图,所以我只需在这里输入pdf文件扩展名。
I’m going to run this code and Python will now have created a plot called "myplot.pdf" in the working directory.
我将运行这段代码,Python现在将在工作目录中创建一个名为“myplot.pdf”的绘图。
The working directory is the directory where you have launched your Python.
工作目录是启动Python的目录。
Let’s see if we can find the plot that we just created.
让我们看看是否能找到我们刚刚创造的情节。
I’m going to launch finder.
我要启动finder。
I know the name of my plot is myplot.pdf.
我知道我的绘图的名称是myplot.pdf。
And we can see that this plot has just been created.
我们可以看到这个情节刚刚被创造出来。
I can double click that to open my pdf.
我可以双击它打开我的pdf。
In this case, my plot is beautifully stored in a pdf file.
在本例中,我的绘图漂亮地存储在pdf文件中。

本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络 » Python数据分析(中英对照)·Customizing Your Plots-自定义绘图

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用

提供最优质的资源集合

立即加入 友好社区
×