将线图添加到imshow并更改轴标记 [英] adding line plot to imshow and changing axis marker
问题描述
我已使用以下代码制作了附件图:
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)
主要区别在于:
-
imshow
可以插值,而pcolormesh
提供矢量输出并且不能插值(即,它绘制许多矩形而不是图像). -
pcolormesh
稍慢一些,因此对于大图像,imshow
是更好的选择. -
imshow
和pcolormesh
对范围的处理略有不同.imshow
是以单元为中心",而pcolormesh
是以网格为中心".这是半个像素的差异,因此在这种情况下您可以忽略它. imshow
会将绘图的宽高比设置为 1,以便 x 方向上的一个单位与 y 方向上的一个单位大小相同.默认情况下,它还翻转 y 轴.
imshow
can interpolate, whilepcolormesh
gives vector output and can't interpolate (i.e. it plots lots of rectangles instead of an image).pcolormesh
is somewhat slower, so for large images,imshow
is a better choice.imshow
andpcolormesh
treat the extents slightly differently.imshow
is "cell-centered" whilepcolormesh
is "mesh-centered". This is a half-pixel difference, so you can ignore it in this case.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屋!