gpt4 book ai didi

python - 限制 matplotlib 热图中的列

转载 作者:行者123 更新时间:2023-12-01 05:48:12 25 4
gpt4 key购买 nike

我正在尝试在 matplotlib 中绘制热图并以此为基础:Moving x-axis to the top of a plot in matplotlib

问题是我只有三列但有 70 行,但当我使用示例时,数字始终有 70 行和 70 列。有谁知道如何限制适合我的数据的列数?

最佳答案

您在我的代码中发现了一个错误。我颠倒了索引(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()

产量

enter image description here

关于python - 限制 matplotlib 热图中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com