在python中绘制多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47449741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:12:38  来源:igfitidea点击:

Plotting multiple lines in python

pythonmatplotlibplotplotlylinechart

提问by user46543

I am new in Python and I want to plot multiple lines in one graph like in the figure below.

我是 Python 新手,我想在一张图中绘制多条线,如下图所示。

enter image description here

enter image description here

I have tried write simple plotting code like this: enter image description here

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

I know these parameters

我知道这些参数

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

But I have a lot of lines such in the first figure, what kind of parameters which I can use to plotting like the first figure.

但是我在第一个图中有很多行,我可以使用什么样的参数来像第一个图一样绘图。

Thank you

谢谢

回答by alexblae

There are many options for line styles and marker in MPL. Have a look here, hereand here.

MPL 中的线条样式和标记有很多选项。看看这里这里这里

For your specific example (I quickly made up some functions and roughly plotted the first few examples):

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

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

This should give you something like this.

这应该给你这样的东西。

enter image description here

enter image description here

回答by Xero Smith

You can define a figure first, then define each plot separately. Below is a minimal exampleenter image description here. You can find more detailed examples here(just focus on the plots).

您可以先定义一个图形,然后分别定义每个图。下面是一个最小的例子enter image description 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')