我正在尝试在matplotlib中绘制热图并将其用作基础:Moving x-axis to the top of a plot in matplotlib
问题是我只有三列但是70行,但是当我使用这个例子时,这个数字总是有70行和70列。有人知道如何限制列数,它们是否适合我的数据?
答案 0 :(得分:2)
您在我的代码中发现了一个错误。我将指数(data.shape
)反转了。
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABC')
row_labels = range(7)
data = np.random.rand(7,3)
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
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()
产量