在matplotlib中imshow的非线性轴 [英] Non-linear axes for imshow in matplotlib
问题描述
我在对数间隔的轴上生成2D数组(例如,使用 logspace(log10(0.95),log10(2.08),n)生成x像素坐标
。
I am generating 2D arrays on log-spaced axes (for instance, the x pixel coordinates are generated using logspace(log10(0.95), log10(2.08), n)
.
我想用原始的imshow显示图像,原始分辨率和缩放(我不需要拉伸它;数据本身已经按日志缩放),但我想在日志轴上添加正确位置的刻度线,标签和线条。我这样做了吗?
I want to display the image using a plain old imshow, in its native resolution and scaling (I don't need to stretch it; the data itself is already log scaled), but I want to add ticks, labels, lines that are in the correct place on the log axes. How do I do this?
理想情况下,我可以使用命令行 axvline(1.5)
,该行将在正确的位置(左起58%),但如果唯一的方法是在logscale坐标和图像坐标之间手动转换,那也没关系。
Ideally I could just use commands line axvline(1.5)
and the line would be in the correct place (58% from the left), but if the only way is to manually translate between logscale coordinates and image coordinates, that's ok, too.
对于线性轴,在imshow调用中使用 extents =
做我想要的,但我没有看到用日志轴做同样事情的方法。
For linear axes, using extents=
in the call to imshow does what I want, but I don't see a way to do the same thing with a log axis.
示例:
from matplotlib.colors import LogNorm
x = logspace(log10(10), log10(1000), 5)
imshow(vstack((x,x)), extent=[10, 1000, 0, 100], cmap='gray', norm=LogNorm(), interpolation='nearest')
axvline(100, color='red')
此示例不起作用,因为extent =仅适用于线性刻度,因此当您将axvline设置为100时,它不会出现在中心。我希望x轴显示10,100,1000和 axvline(100)
将一条线放在100点的中心,而像素保持不变间隔。
This example does not work, because extent= only applies to linear scales, so when you do axvline at 100, it does not appear in the center. I'd like the x axis to show 10, 100, 1000, and axvline(100)
to put a line in the center at the 100 point, while the pixels remain equally spaced.
推荐答案
实际上,它工作正常。我很困惑。
Actually, it works fine. I'm confused.
以前我收到的错误是非线性轴上不支持图像这就是我问这个问题的原因。但现在当我尝试它时,它可以工作:
Previously I was getting errors about "Images are not supported on non-linear axes" which is why I asked this question. But now when I try it, it works:
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(1, 3, 5)
y = np.linspace(0, 2, 3)
z = np.linspace(0, 1, 4)
Z = np.vstack((z, z))
plt.imshow(Z, extent=[10, 1000, 0, 1], cmap='gray')
plt.xscale('log')
plt.axvline(100, color='red')
plt.show()
这比 pcolor()
和 pcolormesh()<更好/ code>因为
This is better than pcolor()
and pcolormesh()
because
- 它不是非常慢而且
- 插入很好而没有误导图像未以原始分辨率显示时的工件。
这篇关于在matplotlib中imshow的非线性轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!