将线图添加到imshow并更改轴标记 [英] adding line plot to imshow and changing axis marker

查看:41
本文介绍了将线图添加到imshow并更改轴标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下代码制作了附件图:

I have made the attached plot using the following codes:

a = 1
theta = np.linspace(0,2*np.pi,101)
x = np.linspace(-3*a,3*a,1001, dtype='complex')
y = np.linspace(-3*a,3*a,1001, dtype='complex')
X,Y = np.meshgrid(x,y)

# come manipulations with V
# (same shape and type as X,Y) not shown here 

plt.subplot(1,2,1)
plt.scatter(a*np.cos(theta), a*np.sin(theta))
plt.imshow(V.real)
plt.colorbar()
plt.subplot(1,2,2)
plt.scatter(a*np.cos(theta), a*np.sin(theta))
plt.imshow(V.imag)
plt.colorbar()

我想做的是:

1)更改图的比例,以使水平轴和垂直轴在-3 * a和3 * a之间变化

1) change the scale of the plot such that the horizontal and vertical axis varies between -3*a and 3*a

2) 绘制圆边界(以原点为中心,半径 = a).现在它显示在左上角,因为绘图的比例从[-3 * a,3 * a]更改为数组大小.

2) plot the circle boundary (centered at the origin with radius = a). Now it appears at the top left, as the scale of the plot is changed from [-3*a,3*a] to that of the size of the array.

推荐答案

通常,您在寻找 imshow extent kwarg.

In general, you're looking for the extent kwarg to imshow.

作为一个简单的例子:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((10, 10))

fig, ax = plt.subplots()
ax.imshow(data, extent=[10, 30, np.pi, -2*np.pi])
plt.show()

就您给出的示例而言:

import numpy as np
import matplotlib.pyplot as plt

a = 1
theta = np.linspace(0, 2*np.pi, 100)

# We could replace the next three lines with:
# y, x = np.mgrid[-3*a:3*a:1000j, -3*a:3*a:1000j]
x = np.linspace(-3*a, 3*a, 1000)
y = np.linspace(-3*a, 3*a, 1000)
x, y = np.meshgrid(x, y)

# Now let's make something similar to your V for this example..
r = np.hypot(x, y)
V = np.cos(3*np.arctan2(y, x)) + np.sin(r) + np.cos(x)*1j * np.cos(r)

def plot(ax, data):
    ax.plot(a*np.cos(theta), a*np.sin(theta), color='black')
    im = ax.imshow(data, extent=[x.min(), x.max(), y.max(), y.min()])
    fig.colorbar(im, ax=ax, shrink=0.5)

fig, (ax1, ax2) = plt.subplots(ncols=2)

ax1.set(title='Real Portion')
plot(ax1, V.real)

ax2.set(title='Imaginary Portion')
plot(ax2, V.imag)

plt.show()

但是,在这种情况下,您也可以考虑使用 pcolormesh.例如,我们可以将 plot 函数更改为:

However, you might also consider using pcolormesh in this case. For example, we could change the plot function to:

def plot(ax, data):
    ax.plot(a*np.cos(theta), a*np.sin(theta), color='black')
    im = ax.pcolormesh(x, y, data)
    ax.set(aspect=1)
    fig.colorbar(im, ax=ax, shrink=0.5)

主要区别在于:

  1. imshow 可以插值,而 pcolormesh 提供矢量输出并且不能插值(即,它绘制许多矩形而不是图像).
  2. pcolormesh 稍慢一些,因此对于大图像, imshow 是更好的选择.
  3. imshow pcolormesh 对范围的处理略有不同.imshow 是以单元为中心",而 pcolormesh 是以网格为中心".这是半个像素的差异,因此在这种情况下您可以忽略它.
  4. imshow 会将绘图的宽高比设置为 1,以便 x 方向上的一个单位与 y 方向上的一个单位大小相同.默认情况下,它还翻转 y 轴.
  1. imshow can interpolate, while pcolormesh gives vector output and can't interpolate (i.e. it plots lots of rectangles instead of an image).
  2. pcolormesh is somewhat slower, so for large images, imshow is a better choice.
  3. imshow and pcolormesh treat the extents slightly differently. imshow is "cell-centered" while pcolormesh is "mesh-centered". This is a half-pixel difference, so you can ignore it in this case.
  4. imshow will set the aspect of the plot to 1, so that one unit in the x-direction is the same size as one unit in the y-direction. It also flips the y-axis, by default.

另一个注意事项:如果您不希望将y轴翻转,请调用 ax.invert_yaxis()或使用 origin ='lower' extent = [xmin,xmax,ymin,ymax] .

One other note: If you'd prefer not to have the y-axis flipped, either call ax.invert_yaxis() or use origin='lower' and extent=[xmin, xmax, ymin, ymax].

这篇关于将线图添加到imshow并更改轴标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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