我想繪制 imshow 熱圖,但最終影像看起來非常小且不成比例。我的資料表示原子之間測量的距離(一個分子的大約 10 個原子到另一個分子的大約 30 個原子) - 結果是array of arrays。我準備了類似的輸出來說明,但是我的原始資料集更大:
import numpy as np
array1 = np.random.randint(20, size=30)
array2 = np.random.randint(20, size=30)
array3 = np.random.randint(20, size=30)
array4 = np.random.randint(20, size=30)
array5 = np.random.randint(20, size=30)
array6 = np.random.randint(20, size=30)
array7 = np.random.randint(20, size=30)
array8 = np.random.randint(20, size=30)
array9 = np.random.randint(20, size=30)
arrayOfArrays = np.array([array1, array2, array3, array4, array5, array6, array7, array8, array9])
然后我想制作熱圖來查看原子之間的距離,所以這是我的代碼:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
im = ax.imshow(arrayOfArrays, origin='upper')
#this is here because I use this approach to define xticks and yticks in my original plot - here I modified the code with "len(range(0,30))" but in my original plot there is number of atoms for which I measured the distances (something like n_atoms=len(dataset1))
n_1=len(range(0,30))
n_2=len(range(0,9))
tick_interval = 1
ax.set_yticks(np.arange(n_2)[::tick_interval])
ax.set_xticks(np.arange(n_1)[::tick_interval])
# colorbar
cbar = fig.colorbar(im)
使用此特定繪圖創建的最終影像看起來已經很小,但帶有原始資料的影像甚至更小,我根本看不到里面的顏色。

我真的很感激任何建議問題出在哪里或應該編輯代碼的哪一部分?我試圖編輯圖片的大小,添加“插值”,“ascpect”...
uj5u.com熱心網友回復:
我喜歡 TomiOck 的答案,但我相信 JohanC 是正確的,因為您有一個資料集,其中的列多于行。這可以通過添加aspect='auto'到imshow命令來解決。
這是我的嘗試:
import numpy as np
nrows = 9
ncols = 30
minv = 0 # inclusive
maxv = 20 # exclusive
arrayofArrays = np.random.randint(low=minv, high=maxv, size=(nrows,ncols))
您可以在numpy一行中生成隨機資料。
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(14,8)) # width and height in inches
im = ax.imshow(arrayOfArrays, origin='upper', aspect='auto', interpolation='None')
n_1 = np.arange(ncols)
n_2 = np.arange(nrows)
tick_interval = 1
ax.set_yticks(n_2[::tick_interval])
ax.set_xticks(n_1[::tick_interval])
# colourbar
cbar = fig.colorbar(im)
該aspect='auto'拉伸你的影像,使得它填補了身影。我還建議使用interpolation='None'絕對確定 matplotlib 不會改變太多顏色(
uj5u.com熱心網友回復:
首先,沒有必要像那樣創建陣列。相反,您可以使用串列理解將所有內容放在一行中:
arrayOfArrays = np.array([np.random.randint(20, size=30) for _ in range(9)])
我知道您可能正在使用資料集,但是,這對于隨機創建資料很有用:3
您的代碼中還有一些其他冗余,您可以使用
np.arange(30)
而不是使用
n_1 = len(range(0,30))
np.arange(n_1)
創建一個從 0 到 30 的整數串列
Matplotlib 圖形調整大小: 您可以調整圖形的大小,繪制的所有內容
fig.size_in_inches(10, 10)
當我在你的代碼中插入這一行并在 google colab 中運行它時,輸出如下:

如果要調整顏色條的大小,請使用 中的shrink引數,fig.colorbar輸出值為 0.3:

這是所有更改的代碼:
import numpy as np
import matplotlib.pyplot as plt
arrayOfArrays = np.array([np.random.randint(20, size=30) for _ in range(9)])
fig, ax = plt.subplots()
im = ax.imshow(arrayOfArrays, origin='upper')
n_1=30
n_2=9
tick_interval = 1
ax.set_yticks(np.arange(n_2)[::tick_interval]) # using np.arange directly
ax.set_xticks(np.arange(n_1)[::tick_interval])
fig.set_size_inches(10, 10) # setting figure size
# colorbar
cbar = fig.colorbar(im, shrink=0.3) # shrink parameter to adjust the size of colorbar
uj5u.com熱心網友回復:
您也可以使用plt.pcolormesh()來做與 Scientist 的回答類似的事情,除非您不必擔心您的縱橫比。默認情況下,imshow將塊設為方形,就像影像中的像素一樣。
代碼改編自科學家:
nrows = 9
ncols = 30
minv = 0 # inclusive
maxv = 20 # exclusive
arrayofArrays = np.random.randint(low=minv, high=maxv, size=(nrows,ncols))
fig, ax = plt.subplots(figsize=(14,8)) # width and height in inches
im = ax.pcolormesh(arrayofArrays)
n_1 = np.arange(ncols)
n_2 = np.arange(nrows)
tick_interval = 1
ax.set_yticks(n_2[::tick_interval])
ax.set_xticks(n_1[::tick_interval])
# colourbar
cbar = fig.colorbar(im)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371226.html
標籤:Python 数组 麻木的 matplotlib 显示
