在热图中显示单词。蟒蛇

时间:2017-06-30 01:00:29

标签: python matplotlib

这是热图的代码。我想知道我是否可以更改每个方格下面的A B C D等字母的数字1,2,3,4。

'''
Most heatmap tutorials I found online use pyplot.pcolormesh with random sets of
data from Numpy; I just needed to plot x, y, z values stored in lists--without
all the Numpy mumbo jumbo. Here I have code to plot intensity on a 2D array, and
I only use Numpy where I need to (pcolormesh expects Numpy arrays as inputs).
'''
import matplotlib.pyplot as plt
import numpy as np

#here's our data to plot, all normal Python lists
x = [0, 1, 2, 3, 4,5]
y = [0, 1, 2, 3, 4,5]

intensity = [
    [5, 10, 15, 20, 25,3],
    [30, 35, 40, 45, 50,23],
    [55, 60, 65, 70, 75,34],
    [80, 85, 90, 95, 100,24],
    [105, 110, 115, 120, 125,23],
    [105, 110, 115, 120, 125,23]
]

#setup the 2D grid with Numpy
x, y = np.meshgrid(x, y)

#convert intensity (list of lists) to a numpy array for plotting
intensity = np.array(intensity)

#now just plug the data into pcolormesh, it's that easy!
plt.pcolormesh(x, y, intensity)
plt.colorbar() #need a colorbar to show the intensity scale
plt.show() #boom 

2 个答案:

答案 0 :(得分:1)

您将需要set_ * ticklabels命令。我在强度设置后更改了您的代码,如下所示:

f, a = plt.subplots()
im = a.pcolormesh(x, y, intensity)
f.colorbar(im, ax = a) #need a colorbar to show the intensity scale
labels = [item.get_text() for item in a.get_xticklabels()]
labels = ['A', 'B', 'C', 'D', 'E', 'F']
a.set_xticklabels(labels) #set xtick
a.set_yticklabels(labels) #set ytick
plt.show() #boom 

该图显示如下:

a busy cat

答案 1 :(得分:1)

最简单,最直接的方法是pyplot.xticks()方法,专门针对x轴上的标签:

x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 2, 3, 4, 5]

# add these lines here    
x_ticks = ['', 'A', 'B', 'C', 'D', 'E']
plt.xticks(x, x_ticks)  

注意:第一个标签设置为空''以说明原点:

enter image description here

参考文档:http://matplotlib.org/2.0.2/api/pyplot_api.html#matplotlib.pyplot.xticks