Matplotlib 绘图如何旋转90度

Matplotlib 绘图如何旋转90度

Matplotlib 中,我们可以使用 plt.subplots 创建一个图像和一个或多个子图。子图默认情况下是水平方向排列的,但有时我们需要调整它们的方向以更好地展示数据。这里我们将讨论如何旋转一个 Matplotlib 绘图 90 度。

阅读更多:Matplotlib 教程

方法一:调整子图排列

我们可以使用 plt.subplots_adjust 函数调整子图的布局,从而旋转图像为横向布局。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 1, figsize=(8, 6))

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Python

上面的代码使用 plt.subplots 创建一个具有两个子图的图像,然后使用 plt.subplots_adjust 函数将子图的间距设置为 0,从而实现了纵向排列的图像。然后我们将整个图像旋转90度:

fig, ax = plt.subplots(2, 1, figsize=(8, 6))

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.subplots_adjust(wspace=0, hspace=0)
fig = plt.figure(figsize=(6, 8))
fig.add_subplot(111)
plt.imshow(fig.canvas.renderer.buffer_rgba(), aspect='auto',
           origin='lower', extent=[0, 1, 0, 1], cmap='gray', alpha=0.5)
plt.axis('off')
plt.show()
Python

这里我们创建了一个新的出图窗口,将整个图像的画布渲染为 RGBA 格式的缓冲区,并调用 plt.imshow 函数将其作为图像展示出来。结果是将整个 Matplotlib 绘图顺时针旋转了 90 度,这使得纵向排列的子图变为横向排列。

方法二:使用 rotate

Matplotlib 提供了许多绘图工具和函数,其中 rotate 函数可以简单地实现旋转图像的功能。我们可以将整个图像作为一个矩形对象,然后将其旋转 90 度以改变其方向。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 1, figsize=(8, 6))

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Python

这个例子与前面的类似,我们使用 plt.subplots 创建了一个具有两个子图的图像。然后我们使用 rotate 函数将整个图像旋转90度:

fig, ax = plt.subplots(2, 1, figsize=(8, 6))

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.subplots_adjust(wspace=0, hspace=0)
plt.gcf().canvas.draw()
plt.gcf().canvas.renderer.rotate(90)

plt.show()
Python

在这里,我们首先使用 plt.gcf() 函数获取当前绘图对象,然后使用 canvas.draw() 函数重新绘制它。接下来我们调用 canvas.renderer.rotate(90) 将整个图像逆时针旋转90度。这里我们使用 renderer 对象来旋转图像。

方法三:使用 numpy 旋转数组

如果你只是希望旋转子图而不是整个图像,可以使用 numpy 库的 rot90 函数。这个函数可以将数组逆时针旋转任意多个90度,因此我们可以通过多次调用它来旋转子图。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 1, figsize=(8, 6))

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Python

与前面的例子相同,我们首先使用 plt.subplots 创建了一个具有两个子图的图像。然后,我们调用 np.rot90 函数将其旋转90度:

fig, ax = plt.subplots(2, 1, figsize=(8, 6))

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.subplots_adjust(wspace=0, hspace=0)
ax[0] = np.rot90(ax[0])
ax[1] = np.rot90(ax[1])
ax[0].set_xlabel('x label')
ax[0].set_ylabel('y label')
plt.show()
Python

在这里,我们首先使用 np.rot90 函数将子图进行逆时针旋转90度。然后,我们将其重新分配给原来的子图对象。最后,我们使用 set_xlabelset_ylabel 函数将轴标签添加到子图中。

总结

无论是调整子图排列、使用 rotate 函数还是使用 numpy 库的 rot90 函数,我们都可以轻松地将 Matplotlib 绘图旋转90度。这对于一些数据展示的情况非常有用,特别是有时我们希望将子图的排列方向进行更改以便更好地展示数据。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册