在matplotlib中调整刻度标签a之间的space

Adjust space between tick labels a in matplotlib

我的热图基于:Heatmap in matplotlib with pcolor?

我查看了 How to change separation between tick labels and axis labels in Matplotlib 但这不是我需要的

如何固定标签的位置使其与刻度对齐?

#!/usr/bin/python

import matplotlib.pyplot as plt
import numpy as np
import random
in_path = '/path/to/data'
in_file = open(in_path,'r').read().split('\r')
wd = '/'.join(in_path.split('/')[:-1]) + '/'
column_labels = [str(random.random()) + '_dsiu' for i in in_file[0].split('\t')[2:]]
row_labels = []


#Organize data for matrix population
D_cyano_counts = {}
for line in in_file[2:]:
    t = line.split('\t')
    D_cyano_counts[(t[0],t[1])] = [int(x) for x in t[2:]]


#Populate matrix
matrix = []
for entry in sorted(D_cyano_counts.items(), key = lambda x: (np.mean([int(bool(y)) for y in x[-1]]), np.mean(x[-1]))):#, np.mean(x[-1]))):
    (taxon_id,cyano), counts = entry
    normalized_counts = []
    for i,j in zip([int(bool(y)) for y in counts], counts):
        if i > 0:
            normalized_counts.append(i * (5  + np.log(j)))
        else:
            normalized_counts.append(0)

    #Labels
    label_type = 'species'
    if label_type == 'species': label = cyano
    if label_type == 'taxon_id': label = taxon_id
    row_labels.append(str(random.random()))

    #Fill in matrix
    matrix.append(normalized_counts)
matrix = np.array(matrix)

#Fig
fig, ax = plt.subplots()
heatmap = ax.pcolor(matrix, cmap=plt.cm.Greens, alpha = 0.7)
#Format
fig = plt.gcf()
#
ax.set_frame_on(False)
#
font = {'size':3}
ax.xaxis.tick_top()
ax.set_xticks([i + 0.5 for i in range(len(column_labels))])
ax.set_yticks([i + 0.5 for i in range(len(row_labels))])
ax.set_xticklabels(column_labels, rotation = (45), fontsize = 10, va='bottom')#, fontweight = 'demi')
ax.set_yticklabels(row_labels, fontsize = 9, fontstyle='italic')

cbar = plt.colorbar(heatmap)
help(ax.set_xticklabels)
ax.margins(x=0.01,y=0.01)
fig.set_size_inches(20, 13)
plt.savefig('figure.png')

在您的情况下,您必须将标签的水平对齐设置为 left。它们默认居中。 来自@Jean-Sébastien 的 The link 包含您的答案

ax.set_xticklabels(column_labels, rotation = (45), fontsize = 10, va='bottom', ha='left')