Python数据分析(中英对照)·Plotting Using Logarithmic Axes-使用对数轴绘图

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

2.3.3: Plotting Using Logarithmic Axes-使用对数轴绘图

In some plots, it’s helpful to have one or both axes be logarithmic.
在某些绘图中,让一个或两个轴都是对数轴是很有帮助的。
This means that for any given point to be plotted, its x or y-coordinate,or both, are transformed using the log function.
这意味着,对于要绘制的任何给定点,其x或y坐标,或两者都使用log函数进行变换。
So what ends up being plotted on the x-axes is not the original x-coordinate, but instead the value of log x.
因此,最终绘制在x轴上的不是原始x坐标,而是对数x的值。
The logarithm is taken by default in base 10.
默认情况下,对数以10为底。
So if, for example, log of 100 is equal to 2,but the base can be specified to be something else as needed.
例如,如果log100等于2,但可以根据需要将基数指定为其他值。
The plt functions that I used to make logarithmic plots are the following–semilogx() plots the x-axes on a log scale and the y in the original scale;
我用来绘制对数图的plt函数如下–semilogx()以对数比例绘制x轴,以原始比例绘制y轴;
semilogy() plots the y-axes on the log scale and the x in the original scale;
符号学()在对数刻度上绘制y轴,在原始刻度上绘制x轴;
loglog() plots both x and y on logarithmic scales.
loglog()以对数标度绘制x和y。
To plot both x and y on the original scales,you can use to plt function we have just discussed, plt.plot.
要在原始比例上绘制x和y,您可以使用我们刚才讨论的plt函数plt.plot。
To understand why logarithmic plots are sometimes useful, let’s take a quick look at the underlying math.
为了理解为什么对数图有时是有用的,让我们快速地看一下基础数学。
Consider a function of the form y is equal to x the power of alpha.
考虑y的函数等于α的α的幂。
y is equal to x to the power of alpha.
y等于x乘以α的幂。
If alpha is equal to 1, that corresponds to a line that goes through the origin.
如果alpha等于1,则对应于穿过原点的直线。
Alpha equal to half gives you the square root, and alpha equal to 2 gives us a parabola.
Alpha等于一半表示平方根,Alpha等于2表示抛物线。
Let’s see what we can do with this equation.
让我们看看这个等式能做什么。
We’re first going to take logs of both sides, which gives us log of y is equal to log of x to alpha.
我们首先要取两边的对数,这使得y的对数等于x的对数乘以α。
You may remember that in the logarithm, we can pull the exponent here to the front of the expression.
您可能还记得,在对数中,我们可以将这里的指数拉到表达式的前面。
Let’s do just that, in which case we end up with alpha being at the front.
让我们这样做,在这种情况下,我们最终以阿尔法在前面结束。
So our alpha ends up being over here.
所以我们的阿尔法就在这里。
In this case, what we have is log y is equal to alpha times log of x.
在这种情况下,我们得到的是logy等于alpha乘以logx。
We can now think about plotting this on a different set of axes,where instead of using our original y, we have log transformed the axes.
我们现在可以考虑在另一组轴上绘制它,在这里,我们不使用原始y轴,而是对轴进行对数变换。
I’m going to call this y prime.
我将称之为y素。
I’m going to do the same for my x.
我也要为我的x做同样的事。
My alpha stays put.
我的阿尔法保持不变。
So my log x becomes x prime.
所以我的log x变成了x素数。
So you’ll see that on these new axes, we have a much simpler equation.
你们会看到,在这些新的轴上,我们有一个简单得多的方程。
Y prime is equal to alpha times x prime.
Y素数等于alpha乘以x素数。
So let’s look at this as a plot.
让我们把它看作一个情节。
Let me erase my starting point here.
让我在这里删除我的起点。
We have our new axes here.
我们这里有我们的新斧子。
We have our x prime, and we have our y prime here.
我们有x素数,这里有y素数。
The shape of this function is going to be simply a line that looks like this.
这个函数的形状就是这样一条线。
In this case, alpha is going to be given by the slope of this line.
在这种情况下,α将由这条线的斜率给出。
So the lesson here is that functions of the form y is equal to x to power alpha show up as straight lines on a loglog() plot.
这里的教训是,形式为y等于x的函数乘以alpha的幂,在loglog()图上显示为直线。
The exponent alpha is given by the slope of the line.
指数α由直线的斜率给出。
Let’s look at an example of a loglog() plot.
让我们看一个loglog()绘图的示例。
I’m going to work here with my previous plot.
我将在这里处理我以前的情节。
Instead of using the plt.plot function, I’m going to replace the plot with loglog() and I will do that for both y1 and y2.
我不使用plt.plot函数,而是将plot替换为loglog(),我将对y1和y2都这样做。
We will then re-run this.
然后我们将重新运行这个。
And in this case, we are plotting the functions on a loglog() scale.
在本例中,我们以loglog()比例绘制函数。
If you wanted even spacing on the x-axes,we can generate numbers that are evenly spaced, not on a linear scale which is what we get from np.linspace,but on the logarithmic scale.
如果您想要在x轴上均匀分布,我们可以生成均匀分布的数字,而不是在线性范围内(这是我们从np.linspace获得的),而是在对数范围内。
To accomplish this, we can use the np.logspace function.
为了实现这一点,我们可以使用np.logspace函数。
I’m going to go back to my plot, but instead of linspace,I replace lin with log.
我将返回我的绘图,但我用log代替linspace。
Now, I need to be careful with the start and end points.
现在,我需要小心起点和终点。
I’m going to start from minus 1.
我从负1开始。
Remember that means that my first point is going to be 0.1.
记住这意味着我的第一个点是0.1。
I’m going to end at 1, which is 10 to 1, meaning the endpoint is equal to 10.
我将以1结束,即10比1,这意味着端点等于10。
In this case, I’m going to modify the number of points.
在这种情况下,我将修改点数。
Let me go with 40.
让我和40一起去。
We can now re-run the plot.
我们现在可以重新运行该绘图。
And here, we see the result of using logspace.
在这里,我们看到了使用logspace的结果。
In this case, all of the points along the x-axis are evenly spaced.
在这种情况下,沿x轴的所有点的间隔都是均匀的。

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

常见问题FAQ

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

提供最优质的资源集合

立即加入 友好社区
×