(Python)使用Matplotlib将x轴移动到绘图顶部

移动前:

(Python)使用Matplotlib将x轴移动到绘图顶部_第1张图片

我们有两种方法可以实现这个目标:

import warnings
warnings.filterwarnings('ignore')


import numpy as np
import matplotlib.pyplot as plt


column_labels = list('ABCD')
row_labels = list('WXYZ')


data = np.random.rand(4, 4)


fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)

# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)

# want a more natural, table-like display
# Method 1:
ax.invert_yaxis()
ax.xaxis.tick_top()
# Method 2:
# ax.xaxis.set_ticks_position('top')


ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)


plt.show()
plt.clf()

移动后:

(Python)使用Matplotlib将x轴移动到绘图顶部_第2张图片

你可能感兴趣的:(Python,python,matplotlib,开发语言)