如何在 matplotlib 中连接两个不同大小的图形?
How to join two figures with different sizes in matplotlib?
我有两个来自 matplotlib 的不同图形。
第一个显示随时间变化的单元格大小:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import mahotas
import random
fig,ax_curves = plt.subplots(figsize=(8,6))
for cell in range(1,11):
ax_curves.set_ylim(0,100)
ax_curves.set_xlim(0,25)
ax_curves.set_xticks(np.arange(0,21))
ax_curves.plot(np.arange(0,21), random.sample(range(40, 61), 21), label = str(cell))
ax_curves.legend(loc='right',frameon=False)
ax_curves.spines['right'].set_visible(False)
ax_curves.spines['top'].set_visible(False)
ax_curves.set_title('Curves')
plt.show()
第二个显示这些细胞在不同时间点的图像:
fig, ax_images =plt.subplots(10,5,figsize=(9, 16))
columns = 5
rows = 10
for column in range(0, columns):
cell = 1
for row in range(0,rows):
ax_images[row, column].axes.get_xaxis().set_visible(False)
ax_images[row, column].tick_params(
axis='y',
which='both',
left=False,
right=False,
labelleft=False)
ax_images[row, 0].set_ylabel('Cell ' + str(cell), rotation = 0, color=tab10.colors[row])
ax_images[row, 0].yaxis.set_label_coords(-0.4,0.5)
if column == 0:
plt.figtext(0.14 , 0.9, '0 hour',fontsize = 20)
img = mahotas.imread('path/to/image_t0.tif')
if column == 1:
plt.figtext(0.28, 0.9, '5 hours',fontsize = 20)
img = mahotas.imread('path/to/image_t5.tif')
if column == 2:
plt.figtext(0.44 , 0.9, '10 hours', fontsize = 20)
img = mahotas.imread('path/to/image_t10.tif')
if column == 3:
plt.figtext(0.60, 0.9, '15 hours',fontsize = 20)
img = mahotas.imread('path/to/image_t15.tif')
if column == 4:
plt.figtext(0.76 , 0.9, '20 hours', fontsize = 20)
img = mahotas.imread('path/to/image_t20.tif')
ax_images[row, column].imshow(img)
cell = cell + 1
plt.figtext(0.5,0.95,'Cell size through time', fontsize = 20, horizontalalignment='center')
plt.show()
我想要"gather"那两个数字(例如左边第一个和右边第二个)。我用 GrisSpec
或 add_subplot
尝试了几个小时,但我失败了...拜托,如果您有解决此问题的任何线索,可以告诉我吗?
此致!
可以使用Matplotlib的图的add_axes
方法:
这里我只是一个 Q&D 示例,你肯定需要调整(很多)才能得到你想要的东西
In [54]: f = plt.figure(figsize=(13,4))
...: f.add_axes((0.10,0.10, 0.25, 0.85));
In [55]: for y in np.linspace(0.87, 0.10, 10):
...: for x in np.linspace(0.40, 0.85, 5):
...: # LL corner, width, height in figure coordinates, 0 ≤ x,y,h,w ≤ 1
...: f.add_axes((x, y, 0.09, 0.08))
感谢@gboffi 的回答,我已经能够解决我的问题了:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import mahotas
import random
f = plt.figure(figsize=(13,4))
ax_curves = f.add_axes((0.10,1.5, 0.33, 0.85))
for cell in range(1,11):
ax_curves.set_ylim(0,100)
ax_curves.set_xlim(0,30)
ax_curves.set_xticks(np.arange(0,21,5))
ax_curves.plot(np.arange(0,21), random.sample(range(40, 61), 21), label = 'Cell ' + str(cell))
ax_curves.legend(loc='right',frameon=False)
ax_curves.spines['right'].set_visible(False)
ax_curves.spines['top'].set_visible(False)
ax_curves.set_title('Curves')
for column in np.linspace(0.5, 0.9, 5):
cell = 1
tab10 = matplotlib.cm.get_cmap('tab10')
for row in np.linspace(2.8, 0.10, 10):
# LL corner, width, height in figure coordinates, 0 ≤ x,y,h,w ≤ 1
ax_images = f.add_axes((column, row, 0.09, 0.27))
ax_images.axes.get_xaxis().set_visible(False)
ax_images.tick_params(
axis='y',
which='both',
left=False,
right=False,
labelleft=False)
if column == 0.5:
ax_images.set_ylabel('Cell ' + str(cell), rotation = 0, color=tab10.colors[cell-1])
ax_images.yaxis.set_label_coords(-0.4,0.5)
else :
pass
if column == 0.5:
plt.figtext(column+0.01 , 3.1, '0 hour',fontsize = 20)
# img = mahotas.imread('path/to/image_t0.tif')
if column == 0.6:
plt.figtext(column, 3.1, '5 hours',fontsize = 20)
# img = mahotas.imread('path/to/image_t5.tif')
if column == 0.7:
plt.figtext(column , 3.1, '10 hours', fontsize = 20)
# img = mahotas.imread('path/to/image_t10.tif')
if column == 0.8:
plt.figtext(column, 3.1, '15 hours',fontsize = 20)
# img = mahotas.imread('path/to/image_t15.tif')
if column == 0.9:
plt.figtext(column , 3.1, '20 hours', fontsize = 20)
# img = mahotas.imread('path/to/image_t20.tif')
# ax[row, column].imshow(img)
cell = cell + 1
plt.figtext(0.5,3.33,'Cell sizes through time', fontsize = 20, horizontalalignment='center')
非常感谢您的帮助!!!
我有两个来自 matplotlib 的不同图形。 第一个显示随时间变化的单元格大小:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import mahotas
import random
fig,ax_curves = plt.subplots(figsize=(8,6))
for cell in range(1,11):
ax_curves.set_ylim(0,100)
ax_curves.set_xlim(0,25)
ax_curves.set_xticks(np.arange(0,21))
ax_curves.plot(np.arange(0,21), random.sample(range(40, 61), 21), label = str(cell))
ax_curves.legend(loc='right',frameon=False)
ax_curves.spines['right'].set_visible(False)
ax_curves.spines['top'].set_visible(False)
ax_curves.set_title('Curves')
plt.show()
第二个显示这些细胞在不同时间点的图像:
fig, ax_images =plt.subplots(10,5,figsize=(9, 16))
columns = 5
rows = 10
for column in range(0, columns):
cell = 1
for row in range(0,rows):
ax_images[row, column].axes.get_xaxis().set_visible(False)
ax_images[row, column].tick_params(
axis='y',
which='both',
left=False,
right=False,
labelleft=False)
ax_images[row, 0].set_ylabel('Cell ' + str(cell), rotation = 0, color=tab10.colors[row])
ax_images[row, 0].yaxis.set_label_coords(-0.4,0.5)
if column == 0:
plt.figtext(0.14 , 0.9, '0 hour',fontsize = 20)
img = mahotas.imread('path/to/image_t0.tif')
if column == 1:
plt.figtext(0.28, 0.9, '5 hours',fontsize = 20)
img = mahotas.imread('path/to/image_t5.tif')
if column == 2:
plt.figtext(0.44 , 0.9, '10 hours', fontsize = 20)
img = mahotas.imread('path/to/image_t10.tif')
if column == 3:
plt.figtext(0.60, 0.9, '15 hours',fontsize = 20)
img = mahotas.imread('path/to/image_t15.tif')
if column == 4:
plt.figtext(0.76 , 0.9, '20 hours', fontsize = 20)
img = mahotas.imread('path/to/image_t20.tif')
ax_images[row, column].imshow(img)
cell = cell + 1
plt.figtext(0.5,0.95,'Cell size through time', fontsize = 20, horizontalalignment='center')
plt.show()
我想要"gather"那两个数字(例如左边第一个和右边第二个)。我用 GrisSpec
或 add_subplot
尝试了几个小时,但我失败了...拜托,如果您有解决此问题的任何线索,可以告诉我吗?
此致!
可以使用Matplotlib的图的add_axes
方法:
这里我只是一个 Q&D 示例,你肯定需要调整(很多)才能得到你想要的东西
In [54]: f = plt.figure(figsize=(13,4))
...: f.add_axes((0.10,0.10, 0.25, 0.85));
In [55]: for y in np.linspace(0.87, 0.10, 10):
...: for x in np.linspace(0.40, 0.85, 5):
...: # LL corner, width, height in figure coordinates, 0 ≤ x,y,h,w ≤ 1
...: f.add_axes((x, y, 0.09, 0.08))
感谢@gboffi 的回答,我已经能够解决我的问题了:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import mahotas
import random
f = plt.figure(figsize=(13,4))
ax_curves = f.add_axes((0.10,1.5, 0.33, 0.85))
for cell in range(1,11):
ax_curves.set_ylim(0,100)
ax_curves.set_xlim(0,30)
ax_curves.set_xticks(np.arange(0,21,5))
ax_curves.plot(np.arange(0,21), random.sample(range(40, 61), 21), label = 'Cell ' + str(cell))
ax_curves.legend(loc='right',frameon=False)
ax_curves.spines['right'].set_visible(False)
ax_curves.spines['top'].set_visible(False)
ax_curves.set_title('Curves')
for column in np.linspace(0.5, 0.9, 5):
cell = 1
tab10 = matplotlib.cm.get_cmap('tab10')
for row in np.linspace(2.8, 0.10, 10):
# LL corner, width, height in figure coordinates, 0 ≤ x,y,h,w ≤ 1
ax_images = f.add_axes((column, row, 0.09, 0.27))
ax_images.axes.get_xaxis().set_visible(False)
ax_images.tick_params(
axis='y',
which='both',
left=False,
right=False,
labelleft=False)
if column == 0.5:
ax_images.set_ylabel('Cell ' + str(cell), rotation = 0, color=tab10.colors[cell-1])
ax_images.yaxis.set_label_coords(-0.4,0.5)
else :
pass
if column == 0.5:
plt.figtext(column+0.01 , 3.1, '0 hour',fontsize = 20)
# img = mahotas.imread('path/to/image_t0.tif')
if column == 0.6:
plt.figtext(column, 3.1, '5 hours',fontsize = 20)
# img = mahotas.imread('path/to/image_t5.tif')
if column == 0.7:
plt.figtext(column , 3.1, '10 hours', fontsize = 20)
# img = mahotas.imread('path/to/image_t10.tif')
if column == 0.8:
plt.figtext(column, 3.1, '15 hours',fontsize = 20)
# img = mahotas.imread('path/to/image_t15.tif')
if column == 0.9:
plt.figtext(column , 3.1, '20 hours', fontsize = 20)
# img = mahotas.imread('path/to/image_t20.tif')
# ax[row, column].imshow(img)
cell = cell + 1
plt.figtext(0.5,3.33,'Cell sizes through time', fontsize = 20, horizontalalignment='center')
非常感谢您的帮助!!!