use list slicing to reverse your data:
data = np.array(data[::-1])
or invert the Y-axis:
ax.invert_yaxis()
题
I've been trying to plot a heatmap with on the x-axis a timespan (with 5 minute increments) and on the y-axis a certain video that has been watched on the internet.
Everything is going well, and the data is good. For instance, a dataset might look like this:
[[0.5, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.5, 0.0, 0.0, 1.5, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
This means there are two rows and 22 columns (0:00:00 - 1:45:00).
I am using this code to plot the heatmap:
def printheatmap(title,names,data):
data = np.array(data)
fig, ax = plt.subplots()
fig = plt.gcf()
fig.set_size_inches(8, 11)
heatmap = ax.pcolormesh(data, cmap=plt.cm.Blues, edgecolors='black')
ax.set_title(title)
ax.set_xticks(np.arange(data.shape[1])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0])+0.5, minor=False)
ax.xaxis.tick_bottom()
ax.set_xticklabels(times, minor=False, weight='ultralight', rotation=40, ha='right')
ax.set_yticklabels(names, minor=False)
ax.grid(False)
plt.colorbar(heatmap)
plt.show()
But for some reason, the second row gets plotted first in the heatmap:
As you can see, the one that starts with (0.5;0;0;1.5) is at the top of the chart with the correct label. Is there any way in which I can 'preserve' the sorting I am doing manually?
The y-axis isn't inverted. With a larger data set, the plot seems to be completely random (but, apparently, the highest scores are at the bottom), while the last row should be:
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0].
Here's another picture. I sorted the data set by date, but it just scrambles it up:
EDIT: Fixed it. Apparently using custom XPath-functions to sort your data isn't really working well with matplotlib. Sorting it manually seemed to do the trick.
解决方案
use list slicing to reverse your data:
data = np.array(data[::-1])
or invert the Y-axis:
ax.invert_yaxis()