在python中绘制多行

时间:2017-11-23 07:24:46

标签: python matplotlib plot plotly linechart

我是Python的新手,我想在一个图中绘制多行,如下图所示。

enter image description here

我试过写这样简单的绘图代码: enter image description here

我知道这些参数

 # red dashes, blue squares and green triangles
    plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')

但是我在第一张图中有很多这样的线,我可以用什么样的参数来绘制第一个图。

谢谢

2 个答案:

答案 0 :(得分:2)

MPL中的线型和标记有很多选项。查看hereherehere

对于您的具体示例(我快速编写了一些函数并粗略绘制了前几个示例):

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()

这应该给你这样的东西。

enter image description here

答案 1 :(得分:1)

您可以先定义图形,然后分别定义每个图形。以下是最小示例enter image description here。您可以找到更详细的示例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')