如何使用matplotlib绘制x和y线?

2024-09-07 01:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

如何绘制x与y线?我的意思是,如果x轴和y轴已经固定了,如何绘制x和y线,就好像这条线的轴是相反的。在

更新: 有人问我为什么不把参数和轴的标签颠倒过来。这是我的理由:这条x对y线只是二维图(主绘图)的一部分,而主轴是用于2D绘图的。更重要的是,在同一个2D图中也有y线和x线。我这样做是因为我想清楚地显示某些线条。在

更新: 下面是我想要的一个例子: what I want

我想在我手动绘制的图形中绘制黑线(实际上我想绘制高斯曲线)。这是时间与电压的关系。我仍然想保留现有的蓝线,我不应该颠倒时间/电压标签。在


Tags: 图形绘图参数时间绘制标签手动what
1条回答
网友
1楼 · 发布于 2024-09-07 01:52:44

您可以在matplotlib中的同一子图中轻松绘制多条曲线。作为示例,请参见以下带注释的代码:

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

# Note that using plt.subplots below is equivalent to using
# fig = plt.figure() and then ax = fig.add_subplot(111)
fig, ax = plt.subplots()
#plot sine wave
ax.plot(t, s, label = "sine wave")

#now create y values for the second plot
y = np.linspace(0, 2, 1000)
#calculate the values for the Gaussian curve
x = 2 * np.exp(-0.5 * np.square(-4 * (y - 1)))
#plot the Gaussian curve
ax.plot(x, y, label = "Gaussian curve")

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

#show the legend
plt.legend()
plt.show()

输出:

{a1}

相关问题 更多 >