Skip to main content

15.7) More Curves


To put two curves on one plot, the easiest way is to combine your two lines within the same plot() command. For example:

x1=[1,2,3,4]
y1=[0.8, 2.1, 1.2, 4.3]
x2=[1.5,2.5,3.5,4.5]
y2=[1.8, 3.1, 2.2, 5.3]
plt.plot(x1, y1, '--o',
         x2, y2, '-D')

If you want to add labels in a legend, it’s easier to separate the plot to add line labels which are then used by the legend function:

plt.plot(x1, y1, '--o', label=‘line1’)
plt.plot(x2, y2, '-D', label=‘line2’)
plt.legend()
# with no arguments, this function will automatically generate the legend.
plt.show()

Plot with two lines and a legend.

Here we have two lines. The first one defined by (x1,y1) is plotted with a dashed line and circles and the second one with a solid line and diamonds.

Define 4 vectors and create a plot with two lines.