Matplotlib:如何获取 window 内图形的大小
Matplotlib: How to get size of a figure within a window
输入x、y、z坐标会输出三个图:一个沿y轴滑动的x-z图,一个沿z轴滑动的x-y图,一个沿x轴滑动的y-z图。我根据用户在滑块工具上滑动相应坐标的百分比来定位线条。见下文(不要对冠状视图和横向视图的切换方式感到惊慌):
但是,如您所见,y 坐标较低,因此该线超出了图形的范围。问题是线条是相对于 window 而不是相对于情节定位的。因此,我想获取 window 内的图形大小来更正此问题。我没有找到任何关于如何在 window 中找到图形尺寸而不是整个 window 的文档——我将如何处理这个问题?谢谢!请参阅下面的代码以查看每个单独的图是如何可视化的:
fig = plt.figure(d+1, figsize = (maxX, maxY))
xCoord = -1
yCoord = -1
if d == 0:
thisSlice = fData[newVoxel.s, :, :, 0]
plt.title("Saggital")
xCoord = newVoxel.t / maxT
yCoord = newVoxel.c / maxC
elif d == 1:
thisSlice = fData[:, newVoxel.t, :, 0]
plt.title("Transverse")
xCoord = newVoxel.s / maxS
yCoord = newVoxel.c / maxC
elif d == 2:
thisSlice = fData[:, :, newVoxel.c, 0]
plt.title("Coronal")
xCoord = newVoxel.s / maxS
yCoord = newVoxel.t / maxT
artists.append(fig.add_artist(lines.Line2D([0, 1], [yCoord, yCoord])))
artists.append(fig.add_artist(lines.Line2D([xCoord, xCoord], [0, 1])))
plt.axis('off')
plt.imshow(thisSlice.T, cmap = 'inferno', origin = 'lower')
可以将图形点转换为坐标轴数据; matplotlib 有一个 entire framework 专门用于此。但是,为什么让你的生活更加困难?我建议围绕绘制图像的轴限制以不同方式构建图形:
import matplotlib.pyplot as plt
#random data
import numpy as np
rng = np.random.default_rng(123)
maxX = rng.integers(10, 20)
maxY = rng.integers(5, 10)
thisSlice = rng.random((maxX, maxY))
def define_figure(d, thisSlice):
fig = plt.figure(d+1, figsize=(maxX, maxY))
ax = fig.add_subplot()
artists = []
my_title = ["Saggital", "Transverse", "Coronal"][d]
ax.set_title(my_title)
ax.axis('off')
img = ax.imshow(thisSlice.T, cmap = 'inferno', origin = 'lower')
view_x_max, view_y_max = thisSlice.shape
viewlimits = [view_x_max, view_y_max]
artists.append(ax.axhline(y=view_y_max//2, xmin=-10, xmax=10, clip_on = False))
artists.append(ax.axvline(x=view_x_max//2, ymin=-10, ymax=10, clip_on = False))
return fig, ax, img, artists, viewlimits
#create figure and capture essential figure elements you will need for later updates
current_figure, current_axis, current_image, current_lineartists, current_viewlimits = define_figure(1, thisSlice)
#now limit the slider range to integer values not exceeding current_viewlimits
#or update the image in axis if the corresponding slider is moved
def update_image(img_object, newimage):
img_object.set_data(newimage.T)
plt.pause(5)
update_image(current_image, rng.random(thisSlice.shape))
plt.pause(5)
plt.show()
输入x、y、z坐标会输出三个图:一个沿y轴滑动的x-z图,一个沿z轴滑动的x-y图,一个沿x轴滑动的y-z图。我根据用户在滑块工具上滑动相应坐标的百分比来定位线条。见下文(不要对冠状视图和横向视图的切换方式感到惊慌):
但是,如您所见,y 坐标较低,因此该线超出了图形的范围。问题是线条是相对于 window 而不是相对于情节定位的。因此,我想获取 window 内的图形大小来更正此问题。我没有找到任何关于如何在 window 中找到图形尺寸而不是整个 window 的文档——我将如何处理这个问题?谢谢!请参阅下面的代码以查看每个单独的图是如何可视化的:
fig = plt.figure(d+1, figsize = (maxX, maxY))
xCoord = -1
yCoord = -1
if d == 0:
thisSlice = fData[newVoxel.s, :, :, 0]
plt.title("Saggital")
xCoord = newVoxel.t / maxT
yCoord = newVoxel.c / maxC
elif d == 1:
thisSlice = fData[:, newVoxel.t, :, 0]
plt.title("Transverse")
xCoord = newVoxel.s / maxS
yCoord = newVoxel.c / maxC
elif d == 2:
thisSlice = fData[:, :, newVoxel.c, 0]
plt.title("Coronal")
xCoord = newVoxel.s / maxS
yCoord = newVoxel.t / maxT
artists.append(fig.add_artist(lines.Line2D([0, 1], [yCoord, yCoord])))
artists.append(fig.add_artist(lines.Line2D([xCoord, xCoord], [0, 1])))
plt.axis('off')
plt.imshow(thisSlice.T, cmap = 'inferno', origin = 'lower')
可以将图形点转换为坐标轴数据; matplotlib 有一个 entire framework 专门用于此。但是,为什么让你的生活更加困难?我建议围绕绘制图像的轴限制以不同方式构建图形:
import matplotlib.pyplot as plt
#random data
import numpy as np
rng = np.random.default_rng(123)
maxX = rng.integers(10, 20)
maxY = rng.integers(5, 10)
thisSlice = rng.random((maxX, maxY))
def define_figure(d, thisSlice):
fig = plt.figure(d+1, figsize=(maxX, maxY))
ax = fig.add_subplot()
artists = []
my_title = ["Saggital", "Transverse", "Coronal"][d]
ax.set_title(my_title)
ax.axis('off')
img = ax.imshow(thisSlice.T, cmap = 'inferno', origin = 'lower')
view_x_max, view_y_max = thisSlice.shape
viewlimits = [view_x_max, view_y_max]
artists.append(ax.axhline(y=view_y_max//2, xmin=-10, xmax=10, clip_on = False))
artists.append(ax.axvline(x=view_x_max//2, ymin=-10, ymax=10, clip_on = False))
return fig, ax, img, artists, viewlimits
#create figure and capture essential figure elements you will need for later updates
current_figure, current_axis, current_image, current_lineartists, current_viewlimits = define_figure(1, thisSlice)
#now limit the slider range to integer values not exceeding current_viewlimits
#or update the image in axis if the corresponding slider is moved
def update_image(img_object, newimage):
img_object.set_data(newimage.T)
plt.pause(5)
update_image(current_image, rng.random(thisSlice.shape))
plt.pause(5)
plt.show()