Python 二维数组 matplotlib 的颜色图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16492830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:48:42  来源:igfitidea点击:

Colorplot of 2D array matplotlib

pythonnumpymatplotlibplot

提问by Slater Victoroff

So, I thought this was going to be really simple, but I've been having a lot of difficult finding exactly what I'm looking for in a comprehensible example.

所以,我认为这会非常简单,但我一直很难在一个易于理解的例子中找到我正在寻找的东西。

Basically I want to make phase plots, so assuming I have a 2d array, how can I get matplotlib to convert this to a plot that I can attach titles, axes, and legends (color bars) to.

基本上我想制作相位图,所以假设我有一个二维数组,我怎样才能让 matplotlib 将它转换为一个可以附加标题、轴和图例(颜色条)的图。

I'm looking for an extremely simple bare bones solution that only uses what is required that will work with any 2D array.

我正在寻找一个非常简单的基本解决方案,它只使用适用于任何二维数组的所需内容。

I'm certain this is simple and I'm just being thick somehow, but I'm really having a lot of trouble with this.

我确定这很简单,而且我只是有点厚,但我真的遇到了很多麻烦。

I have been tooling with the examples, but they don't seem well suited to what I'm trying to do: I like the general appearance of this graph, I'd just like to be able to pass in a 2dArray and have this same result:

我一直在使用这些示例,但它们似乎不太适合我想要做的事情:我喜欢这个图的一般外观,我只是希望能够传入一个 2dArray 并拥有这个相同的结果:

import numpy as np
import matplotlib as ml
import matplotlib.pyplot as plt

H = [[1,2,3,4][5,6,7,8][9,10,11,12][13,14,15,16]]

fig = plt.figure(figsize=(6, 3.2))

ax = fig.add_subplot(111)
ax.set_title('colorMap')
X,Y = np.meshgrid(xedges, yedges)
plt.pcolormesh(X, Y, H)
ax.set_aspect('equal')

cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()

采纳答案by heltonbiker

I'm afraid your posted example is not working, since X and Y aren't defined. So instead of pcolormeshlet's use imshow:

恐怕您发布的示例不起作用,因为未定义 X 和 Y。所以,而不是pcolormesh让我们使用imshow

import numpy as np
import matplotlib.pyplot as plt

H = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12],
              [13, 14, 15, 16]])  # added some commas and array creation code

fig = plt.figure(figsize=(6, 3.2))

ax = fig.add_subplot(111)
ax.set_title('colorMap')
plt.imshow(H)
ax.set_aspect('equal')

cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()

回答by user185160

Here is the simplest example that has the key lines of code:

这是包含关键代码行的最简单示例:

import numpy as np 
import matplotlib.pyplot as plt

H = np.array([[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9, 10, 11, 12],
          [13, 14, 15, 16]])

plt.imshow(H)
plt.show()

enter image description here

在此处输入图片说明