В Python Matplotlib, Как я могу получить тонкие изображения, чтобы ограничить основное изображение? - PullRequest
0 голосов
/ 31 мая 2018

У меня есть 2D матрица Main, матрица Left, высота которой соответствует высоте Main, и матрица Top, ширина которой соответствует ширине Main.Я хотел бы, чтобы у всех 3 были цветные полосы (ограниченные их индивидуальными пределами), и чтобы они были сшиты вместе в 1 изображение, Top вверху, Left слева от Main.

В конце я хотел бы иметь возможность построить весь этот блок на участке других графиков.

До сих пор я пытался axes_grid1, и я не могу получить тонкие матрицы (ицветные полосы), чтобы придерживаться их соответствующих сторон.Должен ли я использовать что-то еще полностью, хотя?

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

# Data
n = 20
Main = np.random.randn(n, n)
Left = np.random.randn(n, 1)
Top  = np.random.randn(n, 2)

# Setup
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
ax_top   = divider.append_axes("top",   1., pad=0.)
ax_left  = divider.append_axes("left",  1., pad=0.)
ax_right = divider.append_axes("right", 1., pad=0.)

# Main
im_main = ax.imshow(Main)
clrbar_right = plt.colorbar(im_main, cax=ax_right)

# Top
im_top  = ax_top.imshow(Top.T)
ax_top_divider = make_axes_locatable(ax_top)
ax_top_top = ax_top_divider.append_axes('top', 1., pad=0.)
ax_top_clrbar = plt.colorbar(im_top, cax=ax_top_top, orientation='horizontal')

# Left
im_left = ax_left.imshow(Left)
ax_left_divider = make_axes_locatable(ax_left)
ax_left_left = ax_left_divider.append_axes('left', 1., pad=0.)
ax_left_clrbar = plt.colorbar(im_left, cax=ax_left_left)
plt.show()

1 Ответ

0 голосов
/ 01 июня 2018

Axes_Grid

Я думаю, вы почти все сделали правильно, но не было необходимости создавать несколько разделителей для добавления осей цветных полос.Мы можем использовать сам оригинальный разделитель.Мне пришлось поиграть с параметром size, равным append_axes, чтобы получить правильный интервал с кодом ниже.О добавлении всего этого блока к участку другого сюжета, который может создать больше проблем, которые я еще не проверял.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

# Data
n = 20

Main = np.random.randn(n, n)
Left = np.random.randn(n, 1)
Top  = np.random.randn(n, 2)

# Setup
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)

# The top and left axes for images
ax_top = divider.append_axes('top',size='10%',pad=0.0)
ax_left = divider.append_axes('left',size='5%', pad=0.)

# The color bar axes
ax_top_top  = divider.append_axes('top', size='5%',pad=0.1)
ax_left_left = divider.append_axes('left',size='5%', pad=0.1)
ax_right = divider.append_axes('right',size='5%', pad=0.1)


# Main
im_main = ax.imshow(Main)
clrbar_right = plt.colorbar(im_main, cax=ax_right)

# Top
im_top  = ax_top.imshow(Top.T)
ax_top_clrbar = plt.colorbar(im_top, cax=ax_top_top, orientation='horizontal')

# Left
im_left = ax_left.imshow(Left)
ax_left_clrbar = plt.colorbar(im_left, cax=ax_left_left)

# Turn off the axis labels for the images if you like
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax_top.axes.get_xaxis().set_visible(False)
ax_top.axes.get_yaxis().set_visible(False)
ax_left.axes.get_xaxis().set_visible(False)
ax_left.axes.get_yaxis().set_visible(False)

# Switch the ticks for the color bars on the left and the top
ax_left_left.yaxis.tick_left()
ax_top_top.xaxis.tick_top()

plt.show()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...