xticks_labels = []
for i, label in enumerate(data.columns):
# Split sample name and condition
sample_name, condition = label.split(' ', 1)
xticks_labels.append(condition) # Only show condition name on the heatmap top
# Set the xticks to show the condition
ax.set_xticklabels(xticks_labels, rotation=90)
Верхние ярлыки. Этот вариант более сложный, поскольку я хочу разместить один ярлык в центре группы столбцов:
# Set the ticks for the sample
# Get the sample names in the order of the sample columns
# Create a secondary x-axis for the sample names
ax2 = ax.twiny()
sample_names = [item.split(" ")[0] for item in data.columns]
ax2.yaxis.tick_left()
ticks = []
labels = []
prev_label = None
for i, label in enumerate(sample_names):
if label != prev_label:
ticks.append(i)
labels.append(label)
prev_label = label
ticks.append(i + 1)
ax2.xaxis.set_minor_locator(FixedLocator(ticks))
ax2.xaxis.set_major_locator(FixedLocator([(t0 + t1) / 2 for t0, t1 in zip(ticks[:-1], ticks[1:])]))
ax2.set_xticklabels(labels, rotation=0)
ax2.tick_params(axis='both', which='major', length=0)
ax2.tick_params(axis='x', which='minor', length=60)
# Show ax2 on top so it doesn't get hidden
ax2.spines['top'].set_position(('outward', 40))
# Adjust the layout to make sure both sets of labels are visible
plt.tight_layout()
# Save the plot to a file
plt.savefig(heatmap_file, dpi=300, bbox_inches='tight')
Если я сделаю вышеописанное, создав вторичную ось для верхних меток, я получу только нижние метки (вторичная ось не отображается).
Если я попытаюсь создать верхние метки, используя одну и ту же ось (используя ax вместо ax2, я увижу верхние метки, но не нижнюю):
ax.yaxis.tick_left()
ticks = []
labels = []
prev_label = None
for i, label in enumerate(sample_names):
if label != prev_label:
ticks.append(i)
labels.append(label)
prev_label = label
ticks.append(i + 1)
ax.xaxis.set_minor_locator(FixedLocator(ticks))
ax.xaxis.set_major_locator(FixedLocator([(t0 + t1) / 2 for t0, t1 in zip(ticks[:-1], ticks[1:])]))
ax.set_xticklabels(labels, rotation=0)
ax.tick_params(axis='both', which='major', length=0)
ax.tick_params(axis='x', which='minor', length=60)
# Show ax2 on top so it doesn't get hidden
ax.spines['top'].set_position(('outward', 40))
# Adjust the layout to make sure both sets of labels are visible
plt.tight_layout()
Как я могу увидеть оба варианта, чтобы сгенерировать что-то подобное?
Я делаю сюжет, используя Python Seaborn: [code]import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import matplotlib.colors as c from matplotlib.patches import Patch from matplotlib.ticker import FixedLocator
[/code] Я хочу добавить разные метки сверху и снизу по оси X, примерно так: Нижние метки: [code]xticks_labels = [] for i, label in enumerate(data.columns): # Split sample name and condition sample_name, condition = label.split(' ', 1) xticks_labels.append(condition) # Only show condition name on the heatmap top
# Set the xticks to show the condition ax.set_xticklabels(xticks_labels, rotation=90) [/code] Верхние ярлыки. Этот вариант более сложный, поскольку я хочу разместить один ярлык в центре группы столбцов: [code]# Set the ticks for the sample # Get the sample names in the order of the sample columns
# Create a secondary x-axis for the sample names ax2 = ax.twiny() sample_names = [item.split(" ")[0] for item in data.columns]
ax2.yaxis.tick_left() ticks = [] labels = [] prev_label = None for i, label in enumerate(sample_names): if label != prev_label: ticks.append(i) labels.append(label) prev_label = label ticks.append(i + 1) ax2.xaxis.set_minor_locator(FixedLocator(ticks)) ax2.xaxis.set_major_locator(FixedLocator([(t0 + t1) / 2 for t0, t1 in zip(ticks[:-1], ticks[1:])])) ax2.set_xticklabels(labels, rotation=0) ax2.tick_params(axis='both', which='major', length=0) ax2.tick_params(axis='x', which='minor', length=60)
# Show ax2 on top so it doesn't get hidden ax2.spines['top'].set_position(('outward', 40))
# Adjust the layout to make sure both sets of labels are visible plt.tight_layout()
# Save the plot to a file plt.savefig(heatmap_file, dpi=300, bbox_inches='tight') [/code] Если я сделаю вышеописанное, создав вторичную ось для верхних меток, я получу только нижние метки (вторичная ось не отображается). Если я попытаюсь создать верхние метки, используя одну и ту же ось (используя ax вместо ax2, я увижу верхние метки, но не нижнюю):[code]ax.yaxis.tick_left() ticks = [] labels = [] prev_label = None for i, label in enumerate(sample_names): if label != prev_label: ticks.append(i) labels.append(label) prev_label = label ticks.append(i + 1) ax.xaxis.set_minor_locator(FixedLocator(ticks)) ax.xaxis.set_major_locator(FixedLocator([(t0 + t1) / 2 for t0, t1 in zip(ticks[:-1], ticks[1:])])) ax.set_xticklabels(labels, rotation=0) ax.tick_params(axis='both', which='major', length=0) ax.tick_params(axis='x', which='minor', length=60)
# Show ax2 on top so it doesn't get hidden ax.spines['top'].set_position(('outward', 40))
# Adjust the layout to make sure both sets of labels are visible plt.tight_layout() [/code] Как я могу увидеть оба варианта, чтобы сгенерировать что-то подобное? [img]https://i.sstatic.net/8MnpNeaT.png [/img]
Я делаю сюжет, используя Python Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.colors as c
from matplotlib.patches import Patch
from matplotlib.ticker import FixedLocator
Диаграммы рассеяния с множеством делений по оси Y имеют большие пробелы сверху и снизу, как вы можете видеть по линиям сетки. Как удалить пробелы сверху и снизу диаграммы рассеяния Seaborn?
Диаграммы рассеяния с множеством делений по оси Y имеют большие пробелы сверху и снизу, как вы можете видеть по линиям сетки. Как удалить пробелы сверху и снизу диаграммы рассеяния Seaborn?
Я просто хочу создать ящик в javafx. В этом ящике есть два разных изображения (одного размера) сверху и снизу (не обязательно быть только сверху или снизу, потому что ящик может вращаться, просто на двух противоположных сторонах). Точно так же, как...
Я новичок в разработке для iOS и пытаюсь загрузить веб-сайт с помощью WKWebView. Однако я получаю неожиданный запас в верхней части изображения. Когда я прокручиваю веб-страницу, поля заполняются, но когда я дохожу до конца страницы, внизу...