且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 matplotlib 中使用绘图、轴或图形绘制绘图有什么区别?

更新时间:2023-02-20 16:26:55

方法一

plt.plot(x, y)

这使您可以仅绘制一个具有 (x,y) 坐标的图形.如果你只想得到一个图形,你可以使用这种方式.

This lets you plot just one figure with (x,y) coordinates. If you just want to get one graphic, you can use this way.

方法二

ax = plt.subplot()
ax.plot(x, y)

这让您可以在同一窗口中绘制一个或多个图形.在编写它时,您将只绘制一个图形,但您可以制作如下内容:

This lets you plot one or several figure(s) in the same window. As you write it, you will plot just one figure, but you can make something like this:

fig1, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

您将在同一个窗口上绘制 4 个名为 ax1、ax2、ax3 和 ax4 的图形.在我的例子中,这个窗口将被分为 4 个部分.

You will plot 4 figures which are named ax1, ax2, ax3 and ax4 each one but on the same window. This window will be just divided in 4 parts with my example.

方法三

fig = plt.figure()
new_plot = fig.add_subplot(111)
new_plot.plot(x, y)

我没用过,但你可以找到文档.

I didn't use it, but you can find documentation.

示例:

import numpy as np
import matplotlib.pyplot as plt

# Method 1 #

x = np.random.rand(10)
y = np.random.rand(10)

figure1 = plt.plot(x,y)

# Method 2 #

x1 = np.random.rand(10)
x2 = np.random.rand(10)
x3 = np.random.rand(10)
x4 = np.random.rand(10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
y3 = np.random.rand(10)
y4 = np.random.rand(10)

figure2, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
ax1.plot(x1,y1)
ax2.plot(x2,y2)
ax3.plot(x3,y3)
ax4.plot(x4,y4)

plt.show()

其他示例: