在 python 中绘制多条线
Plotting multiple lines in python
我是 Python 的新手,我想在一张图中绘制多条线,如下图所示。
我试过像这样编写简单的绘图代码:
我知道这些参数
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
但是我在第一个图中有很多这样的线,我可以使用什么样的参数来像第一个图那样绘图。
谢谢
MPL 中有很多线型和标记选项。看看here, here and here.
对于你的具体例子(我很快编了一些函数,粗略地绘制了前几个例子):
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(6)
fig=plt.figure()
fig.show()
ax=fig.add_subplot(111)
ax.plot(x,x,c='b',marker="^",ls='--',label='GNE',fillstyle='none')
ax.plot(x,x+1,c='g',marker=(8,2,0),ls='--',label='MMR')
ax.plot(x,(x+1)**2,c='k',ls='-',label='Rand')
ax.plot(x,(x-1)**2,c='r',marker="v",ls='-',label='GMC')
ax.plot(x,x**2-1,c='m',marker="o",ls='--',label='BSwap',fillstyle='none')
ax.plot(x,x-1,c='k',marker="+",ls=':',label='MSD')
plt.legend(loc=2)
plt.draw()
这应该会给你这样的东西。
您可以先定义一个图形,然后分别定义每个图。下面是一个最小的例子. You can find more detailed examples here(只关注情节)。
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(1, 10, 1000)
plt.figure(figsize=(10, 6))
line1, = plt.plot(t, np.sin(t * 2 * np.pi), 'b-', label='$sin(t)$')
line2, = plt.plot(t, np.cos(t * 2 * np.pi/2), 'r--', label='$sin(t)$')
line3, = plt.plot(t, (np.sin(t * 2 * np.pi))**2, 'k.-', label='$sin(t)$')
plt.legend(loc='upper right')
我是 Python 的新手,我想在一张图中绘制多条线,如下图所示。
我试过像这样编写简单的绘图代码:
我知道这些参数
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
但是我在第一个图中有很多这样的线,我可以使用什么样的参数来像第一个图那样绘图。
谢谢
MPL 中有很多线型和标记选项。看看here, here and here.
对于你的具体例子(我很快编了一些函数,粗略地绘制了前几个例子):
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(6)
fig=plt.figure()
fig.show()
ax=fig.add_subplot(111)
ax.plot(x,x,c='b',marker="^",ls='--',label='GNE',fillstyle='none')
ax.plot(x,x+1,c='g',marker=(8,2,0),ls='--',label='MMR')
ax.plot(x,(x+1)**2,c='k',ls='-',label='Rand')
ax.plot(x,(x-1)**2,c='r',marker="v",ls='-',label='GMC')
ax.plot(x,x**2-1,c='m',marker="o",ls='--',label='BSwap',fillstyle='none')
ax.plot(x,x-1,c='k',marker="+",ls=':',label='MSD')
plt.legend(loc=2)
plt.draw()
这应该会给你这样的东西。
您可以先定义一个图形,然后分别定义每个图。下面是一个最小的例子
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(1, 10, 1000)
plt.figure(figsize=(10, 6))
line1, = plt.plot(t, np.sin(t * 2 * np.pi), 'b-', label='$sin(t)$')
line2, = plt.plot(t, np.cos(t * 2 * np.pi/2), 'r--', label='$sin(t)$')
line3, = plt.plot(t, (np.sin(t * 2 * np.pi))**2, 'k.-', label='$sin(t)$')
plt.legend(loc='upper right')