【发布时间】:2013-01-02 14:14:59
【问题描述】:
基于this question about heatmaps in matplotlib,我想将 x 轴标题移到绘图顶部。
import matplotlib.pyplot as plt
import numpy as np
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[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.set_label_position('top') # <-- This doesn't work!
ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
但是,调用matplotlib's set_label_position(如上所述)似乎没有达到预期的效果。这是我的输出:
我做错了什么?
【问题讨论】:
标签: python matplotlib plot data-visualization