2016-03-04 68 views
0

我正在讀取文本文件,並從該文本文件中找到想要生成matplotlib的熱圖。這是到目前爲止我的代碼:使用Matplotlib而不使用pcolor/pmesh的熱圖

import matplotlib.pyplot as plt 
import numpy as np 
import math 
from matplotlib import colors 
file_object = open("tokillamockingbird.txt", "r", encoding="utf-8") 
reading = file_object.read() 
new_read = reading.strip() 
someWord = '' 
list_words = [] 
for letter in new_read: 
if letter in ",!.?": 
    list_words.append(letter) 
lenListWords = len(list_words) 
lengthOfPunctuation = math.sqrt(lenListWords) 
# generate a scaled plot for this 
data = np.random.rand(lengthOfPunctuation,lengthOfPunctuation) 
fig, axes = plt.subplots() 

到這裏,我堅持,我想用RGB方法matplotlib到specifiy每種顏色一個「盒子」的比例情節了。

當我運行這個它給我這樣的畫面:

Matplotlib output

因此,我怎麼一個特定的顏色添加到特定的盒子? (如果這是4x4將會有16個盒子,並且我想知道如何爲每個盒子添加顏色)

+0

您能否提供一些數據並更詳細地描述您的預期結果? – Cleb

回答

0

您沒有使用正確的命令來生成熱圖,而是生成了2D圖。如果你看看this post,你會發現生成它非常簡單。您可以使用(除其他事項外)

plt.imshow(heatmap) 
plt.show() 

,而不是

plt.plot(x,y) 
plt.show() 

另外,如果你沒有固定的使用Matplotlib,您可以使用Seaborn,這是另一種Python包,用於繪圖。對於涉及相關性和統計分析的基於熱圖的圖形來說非常方便。

+0

對您有幫助嗎? –