Imshow 带有数组数组的热图 - 最终问题 plot/image
Imshow heatmap with array of arrays - problem with final plot/image
我想绘制 imshow 热图,但最终图像看起来很小且不成比例。
我的数据表示原子之间测量的距离(一个分子的大约 10 个原子到另一个分子的大约 30 个原子)- 结果是 数组数组 。我准备了类似的输出用于说明,但是我的原始数据集更大:
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”...
首先,没有必要像那样创建数组。相反,您可以使用列表推导式在一行中获取所有内容:
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 中 运行 时,输出如下:
如果要调整颜色条的大小,请使用 fig.colorbar
中的 shrink
参数,值为 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
我喜欢 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 不会改变太多颜色 (https://matplotlib.org/stable/gallery/images_contours_and_fields/interpolation_methods.html)。
您也可以使用 plt.pcolormesh()
来做与科学家的回答类似的事情,只是您不必担心纵横比。默认情况下,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)
我想绘制 imshow 热图,但最终图像看起来很小且不成比例。 我的数据表示原子之间测量的距离(一个分子的大约 10 个原子到另一个分子的大约 30 个原子)- 结果是 数组数组 。我准备了类似的输出用于说明,但是我的原始数据集更大:
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”...
首先,没有必要像那样创建数组。相反,您可以使用列表推导式在一行中获取所有内容:
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 中 运行 时,输出如下:
如果要调整颜色条的大小,请使用 fig.colorbar
中的 shrink
参数,值为 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
我喜欢 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 不会改变太多颜色 (https://matplotlib.org/stable/gallery/images_contours_and_fields/interpolation_methods.html)。
您也可以使用 plt.pcolormesh()
来做与科学家的回答类似的事情,只是您不必担心纵横比。默认情况下,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)