在matplotlib中使用log2比例绘制方轴图 [英] making square axes plot with log2 scales in matplotlib

查看:445
本文介绍了在matplotlib中使用log2比例绘制方轴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用matplotlib制作一个方轴散点图.通常使用set_scale("log")效果很好,但将我限制为log10.我想在log2中进行绘图.我在这里看到了解决方案:如何生成指数级缩放的轴?

I'd like to make a square axis scatter plot with matplotlib. Normally using set_scale("log") works great, but it limits me to log10. I'd like to make the plot in log2. I saw the solution here: How to produce an exponentially scaled axis?

但是它很复杂,如果您的数组中有0个值,则无法使用,我这样做.我只想像其他numpy函数一样忽略它们.

but it is quite complicated and does not work if you have 0 values in your arrays, which I do. I'd like to simply ignore those like other numpy functions do.

例如:

log2scatter(data1, data2)

其中data1和data2包含0的x和y轴上应具有对数刻度,对数间隔为刻度.就像log10一样,除了log2 ...

where data1 and data2 contain 0s should have a logarithmic scale on the x and y axis, with logarithmic spaced ticks. Just like log10, except log2...

谢谢.

推荐答案

只需指定basex=2basey=2.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set_xscale('log', basex=2)
ax.set_yscale('log', basey=2)

ax.plot(range(1024))
plt.show()

对于过零行为,您所指的是对称对数"图(也称为符号对数").无论值多少,都不会过滤掉数据,它只是接近0的线性图,而其他任何地方都是对数图.变化的是比例,而不是数据.

For the zero-crossing behavior, what you're referring to is a "Symmetric Log" plot (a.k.a. "symlog"). For whatever it's worth, data isn't filtered out, it's just a linear plot near 0 and a log plot everywhere else. It's the scale that changes, not the data.

通常情况下,您只需要执行ax.set_xscale('symlog', basex=2),但是对于符号图而言,使用非10的基数似乎是错误的.

Normally you'd just do ax.set_xscale('symlog', basex=2) but using a non-10 base appears to be buggy at the moment for symlog plots.

修改: !该错误似乎是由于一个经典错误导致的:使用可变的默认参数.
我已经提交了一个错误报告,但是如果您想对其进行修复,则需要在SymmetricalLogLocator__init__方法中,对第1376行的lib/matplotlib/ticker.py进行较小的编辑.

Edit: Heh! The bug appears to be due to a classic mistake: using a mutable default argument.
I've filed a bug report, but if you feel like fixing it, you'll need to make a minor edit to lib/matplotlib/ticker.py, around line 1376, in the __init__ method of SymmetricalLogLocator.

代替

def __init__(self, transform, subs=[1.0]):
    self._transform = transform
    self._subs = subs
    ...

将其更改为类似于以下内容的

Change it to something similar to:

def __init__(self, transform, subs=None):
    self._transform = transform
    if subs is None:
        self._subs = [1.0]
    else:
        self._subs = subs
    ....

所做的更改后,它的行为符合预期...

With that change made, it behaves as expected...

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.set_xscale('symlog', basex=2)
ax.set_yscale('symlog', basey=2)

x = np.arange(-1024, 1024)
ax.plot(x, x)

plt.show()

这篇关于在matplotlib中使用log2比例绘制方轴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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