1

I am trying to plot a heatmap in matplotlib and use this as a basis: Moving x-axis to the top of a plot in matplotlib

The problem is that I have just three columns but 70 rows but the figure always has 70 rows and 70 columns when I use the example. Does anybody know how to limit the amount of columns, that they fit my data?

Community
  • 1
  • 1
Jack Smith
  • 413
  • 1
  • 5
  • 14

1 Answers1

2

You found a bug in my code. I had the indices (for data.shape) reversed.

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()

yields

enter image description here

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677