在matplotlib中,我可以使用pyplot.xscale()
或Axes.set_xscale()
设置轴缩放。这两个函数都接受三种不同的尺度:'linear'
| 'log'
| 'symlog'
。
'log'
和'symlog'
有什么区别?在我做的一个简单的测试中,它们看起来完全一样。
我知道文档上说它们接受不同的参数,但我仍然不明白它们之间的区别。有人能解释一下吗?答案将是最好的,如果它有一些示例代码和图形!(还有:“symlog”这个名字是从哪里来的?)
发布于 2010-08-18 22:29:31
我终于抽出时间做了一些实验,以了解它们之间的区别。这是我的发现:
log
只允许正值,并允许您选择如何处理负值( clip
).symlog
或mask
表示对称对数,允许正负values.symlog
允许在绘图内设置围绕零的范围将是线性的,而不是logarithmic.我认为有了图形和示例,一切都会变得更容易理解,所以让我们尝试一下:
import numpy
from matplotlib import pyplot
# Enable interactive mode
pyplot.ion()
# Draw the grid lines
pyplot.grid(True)
# Numbers from -50 to 50, with 0.1 as step
xdomain = numpy.arange(-50,50, 0.1)
# Plots a simple linear function 'f(x) = x'
pyplot.plot(xdomain, xdomain)
# Plots 'sin(x)'
pyplot.plot(xdomain, numpy.sin(xdomain))
# 'linear' is the default mode, so this next line is redundant:
pyplot.xscale('linear')
# How to treat negative values?
# 'mask' will treat negative values as invalid
# 'mask' is the default, so the next two lines are equivalent
pyplot.xscale('log')
pyplot.xscale('log', nonposx='mask')
# 'clip' will map all negative values a very small positive one
pyplot.xscale('log', nonposx='clip')
# 'symlog' scaling, however, handles negative values nicely
pyplot.xscale('symlog')
# And you can even set a linear range around zero
pyplot.xscale('symlog', linthreshx=20)
为了完整起见,我使用了以下代码来保存每个图形:
# Default dpi is 80
pyplot.savefig('matplotlib_xscale_linear.png', dpi=50, bbox_inches='tight')
请记住,您可以使用以下命令更改图形大小:
fig = pyplot.gcf()
fig.set_size_inches([4., 3.])
# Default size: [8., 6.]
(如果你不确定我是否回答了自己的问题,请阅读this)
发布于 2010-07-22 12:58:51
symlog类似于log,但允许您定义一个接近零的值范围,在该范围内绘图是线性的,以避免让绘图在零附近走向无穷大。
来自http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_xscale
在对数图中,你永远不能有一个零值,如果你有一个接近于零的值,它将从你的图的底部向下尖峰(无限向下),因为当你取“log (接近零)”时,你会得到“接近负无穷大”。
当你想要一个对数图的时候,symlog可以帮助你,但是当这个值有时会下降到零,但是你仍然希望能够以一种有意义的方式在图上显示出来。如果你需要symlog,你会知道的。
发布于 2019-10-21 05:04:16
当symlog是必需的时,下面是一个行为示例:
初始绘图,未缩放。请注意x~0处有多少点聚类
ax = sns.scatterplot(x= 'Score', y ='Total Amount Deposited', data = df, hue = 'Predicted Category')
[
‘
对数缩放绘图。一切都崩溃了。
ax = sns.scatterplot(x= 'Score', y ='Total Amount Deposited', data = df, hue = 'Predicted Category')
ax.set_xscale('log')
ax.set_yscale('log')
ax.set(xlabel='Score, log', ylabel='Total Amount Deposited, log')
‘
为什么它会崩溃?因为x轴上的一些值非常接近或等于0。
符号对数比例图。一切都是正常的。
ax = sns.scatterplot(x= 'Score', y ='Total Amount Deposited', data = df, hue = 'Predicted Category')
ax.set_xscale('symlog')
ax.set_yscale('symlog')
ax.set(xlabel='Score, symlog', ylabel='Total Amount Deposited, symlog')
https://stackoverflow.com/questions/3305865
复制相似问题