Colormaps for 2D arrays in Matplotlib
This tutorial shows how to use matplotlib.pyplot.imshow()
the and matplotlib.pyplot.pcolormesh()
methods in Python to generate colorplots of 2D arrays.
In Matplotlib, we use matplotlib.pyplot.imshow()
the method to plot 2D arrays.
matplotlib.pyplot.imshow()
The method takes a 2D array as input and renders the given array as a raster image.
matplotlib.pyplot.imshow()
Syntax
matplotlib.pyplot.imshow(X,
cmap=None,
norm=None,
aspect=None,
interpolation=None,
alpha=None,
vmin=None,
vmax=None,
origin=None,
extent=None, *,
filternorm=True,
filterrad=4.0,
resample=None,
url=None,
data=None,
**kwargs)
matplotlib.pyplot.imshow()
Example
import numpy as np
import matplotlib.pyplot as plt
X = np.random.randint(256, size=(10, 10))
fig = plt.figure(figsize=(8, 6))
plt.imshow(X)
plt.title("Plot 2D array")
plt.show()
It plots a 2D array of numpy.random.randint()
size created using 10*10
. By default, the values are viridis
mapped using a colormap.
We can change the colormap by imshow()
setting cmap
the parameter in the method.
import numpy as np
import matplotlib.pyplot as plt
X = np.random.randint(256, size=(10, 10))
fig = plt.figure(figsize=(8, 6))
plt.imshow(X, cmap="inferno")
plt.title("Plot 2D array")
plt.colorbar()
plt.show()
It shows inferno
a 2D array plot with a colormap. We can also see a colorbar on the right side of the plot, which tells us which values in the array are mapped to which colors.
In Matplotlib, we use matplotlib.pyplot.pcolormesh()
the method to plot 2D arrays.
matplotlib.pyplot.pcolormesh()
The function creates a pseudocolor plot in Matplotlib. It matplotlib.pyplot.pcolor()
is similar to the function.
import numpy as np
import matplotlib.pyplot as plt
X = np.random.randint(256, size=(10, 10))
fig = plt.figure(figsize=(8, 6))
plt.pcolormesh(X, cmap="plasma")
plt.title("Plot 2D array")
plt.colorbar()
plt.show()
It plots a 2D array of numpy.random.randint()
size created using 10*10
and plasma
a colormap. The colorbar on the right represents the colors assigned to different value ranges.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Using Latex formulas in Matplotlib
Publish Date:2025/05/04 Views:62 Category:Python
-
This tutorial explains how we can display LaTex formulas or equations in Matplotlib. LaTex Writing formulas in Matplotlib Python import math import numpy as np import matplotlib.pyplot as plt x = np . linspace( 0 , 2 * math . pi, 100 ) y =
Matplotlib Candlestick Chart
Publish Date:2025/05/04 Views:72 Category:Python
-
In this demonstration, we will introduce candlestick charts or plots and see how to mplfinance create an OHLC (Open, High, Low, Close) candlestick chart using the module in Python Matplotlib. mplfinance Create candlestick charts using the M
Matplotlib Tutorial - Axis Titles
Publish Date:2025/05/04 Views:53 Category:Python
-
In this tutorial, we will learn about axis titles in Matplotlib. Matplotlib axis titles matplotlib . pyplot . title(label, fontdict = None , loc = None , ** kwargs) It is used to set the title of the current axis. parameter name Data Types
Matplotlib Tutorial - Pie Chart
Publish Date:2025/05/04 Views:144 Category:Python
-
We will learn about Pie Charts in this tutorial. # -*- coding: utf-8 -*- import matplotlib.pyplot as plt x = np . array([ 15 , 25 , 30 , 40 ]) label = [ "France" , "Germany" , "Uk" , "US" ] plt . pie(x, labels = label) plt . show() gramm
如何在 Matplotlib Pyplot 中显示网格
Publish Date:2024/02/04 Views:146 Category:Python
-
本文演示了如何在 Python Matplotlib 中在一个图上画一个网格。使用 grid()函数来绘制网格,并解释了如何改变网格颜色和线条类型。
在 Matplotlib 中的图中添加文字
Publish Date:2024/02/04 Views:182 Category:Python
-
本教程展示了我们如何使用 plt.text()方法在 Matplotlib 中为图或轴添加文字。
如何在 Matplotlib 中的多个线条之间进行填充
Publish Date:2024/02/04 Views:213 Category:Python
-
`fill_between()` 每次只能填充两条线之间的区域,但是我们可以选择一对行来填充多个线条之间的区域。
如何在 Matplotlib 中画一条任意线
Publish Date:2024/02/04 Views:169 Category:Python
-
本教程讲解了我们如何在 Matplotlib 中使用 matplotlib.pyplot.plot()、matplotlib.pyplot.vlines()、matplotlib.pyplot.hlines()方法和 matplotlib.collection.LineCollection 绘制任意线条。
Pandas 在 Matplotlib 柱状图上绘制多列图
Publish Date:2024/02/04 Views:195 Category:Python
-
在本教程中,我们将探讨如何使用 `DataFrame` 对象的 `plot()` 方法在柱状图上绘制多列。