w3resource

Matplotlib Basic: Plot two or more lines with different styles

Matplotlib Basic: Exercise-7 with Solution

Write a Python program to plot two or more lines with different styles.

Sample Solution:

Python Code:

import matplotlib.pyplot as plt
# line 1 points
x1 = [10,20,30]
y1 = [20,40,10]
# line 2 points
x2 = [10,20,30]
y2 = [40,10,30]
# Set the x axis label of the current axis.
plt.xlabel('x - axis')
# Set the y axis label of the current axis.
plt.ylabel('y - axis')
# Plot lines and/or markers to the Axes.
plt.plot(x1,y1, color='blue', linewidth = 3,  label = 'line1-dotted',linestyle='dotted')
plt.plot(x2,y2, color='red', linewidth = 5,  label = 'line2-dashed', linestyle='dashed')
# Set a title 
plt.title("Plot with two or more lines with different styles")
# show a legend on the plot
plt.legend()
# function to show the plot
plt.show()

Sample Output:

Matplotlib Basic: Plot two or more lines with different styles

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to plot two or more lines with legends, different widths and colors.
Next: Write a Python program to plot two or more lines and set the line markers.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/graphics/matplotlib/basic/matplotlib-basic-exercise-7.php