Matplotlib的“符号"选项:如何防止曲线“回来"? [英] Matplotlib's "symlog" option: how to prevent curves that "come back"?

查看:38
本文介绍了Matplotlib的“符号"选项:如何防止曲线“回来"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制范围为 x 的数据,从 -1000 到 1000.但我只对 x = -1 和 0 之间的值感兴趣.

我还想在 x 对数刻度上绘图.但是由于x值为负,所以我不能使用xscale("log").我可以使用xscale("symlog"),这是我想要的行为.

不幸的是,符号日志"似乎已损坏.我不能使用小于2的linthreshx参数(默认值?).但是,由于我对-1到0的x值感兴趣,因此必须使用该参数并将其设置为1e-5或更小.

如果将linthreshx设置为小于1的值,则情节中断.这是一个简单的例子,取自

现在已修复.我附上了一张图片,显示了上一个片段的正确结果.

I'm plotting data which extent, in x, from -1000 to 1000. But I'm only interested in the values between x = -1 and 0.

I also want to plot on an x log scale. But since the x-values are negative, I cannot use xscale("log"). I can use xscale("symlog") though and this is the behaviour I want.

Unfortunately, "symlog" seems to be broken. I cannot use the linthreshx argument with a value of less then 2 (the default?). But since I'm interested in x values from -1 to 0, I have to use that argument and set it to something like 1e-5 or even smaller.

If I set linthreshx to something smaller then 1, the plot breaks. Here is a simple example, taken from What is the difference between 'log' and 'symlog'?:

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))

pyplot.axis([-60,60,-1.5,1.5])

pyplot.xscale('symlog', linthreshx=0.1)

Running that you'll see what I mean by the curve "coming back"... Here is the resulting image:

The problem seems to be that, on the x-axis, 0 is actually 10^0 = 1, not 0. Putting something smaller then 1 will make the line go back and the axis values are wrong (when hovering with the mouse and getting the x value).

I might not be using the right tool though, but how to achieve what I want? I want the x axis to look like: -10^2 -10^1 -10^0 -10^-1 -10^-2 -10^-3 ... [up to my defined minimum exponent] ... 10^-3 10^-2 10^-1 10^0 10^1 10^2

Thank you

解决方案

This was actually a bug in matplotlib.

See https://github.com/matplotlib/matplotlib/issues/396

It is now fixed. I'm attaching a picture showing the correct result of the previous snippet.

这篇关于Matplotlib的“符号"选项:如何防止曲线“回来"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆