Python Line Plot Using Matplotlib
In this article, you will learn to draw simple to complex line plotting using the Python Matplotlib Library.
Line plots generate a graph by drawing line segments between two points. It shows the frequency of data along a number line. It is a quick, simple way to organize data and best used when comparing fewer than 25 different numbers.
The Matplotlib is a Python plotting module. It can create many types of plots in which a line plot is basically in use. It provides plot(x,y) method to create a line plot.
Python Basic Line Plot
Let's draw a simple line plot. For this, we will take two variables for x and y coordinates and assign values. We will pass these variables as arguments to the plot() method.
import matplotlib.pyplot as plt
# Data to plot
x = [2, 3, 4, 5, 6, 8, 9, 10]
y = [5, 15, 6, 12, 10, 25, 12, 14]
plt.plot(x,y)
plt.show()
Python Line Plot With Markers
The given code specifies a marker symbol in the line-specification input argument. For this, we have set the marker property with circular markers. We have also specified the color of the plotting.
import matplotlib.pyplot as plt
# Data to plot
x = [2, 3, 4, 5, 6, 8, 9, 10]
y = [5, 15, 6, 12, 10, 25, 12, 14]
plt.plot(x,y, marker = 'o', color='#7d0552')
plt.show()
Python Line Plot With Line Styles
In the above example, we have set the markers and color properties. Similarly, we can also set the property of line style, like -
import matplotlib.pyplot as plt
# Data to plot
x = [2, 3, 4, 5, 6, 8, 9, 10]
y = [5, 15, 6, 12, 10, 25, 12, 14]
plt.plot(x,y, marker = 'o', color='#f70d1a', linestyle = ':')
plt.show()
Python Multiple Lines Plot
A multiple line graph shows the relationship between independent and dependent values of multiple sets of data. It is best for comparing different sets of data. Here, we have taken three different data sets and plotted them.
import matplotlib.pyplot as plt
# Data to plot
x = [2, 3, 4, 5, 6, 8, 9, 10]
y1 = [5, 15, 6, 12, 10, 25, 12, 14]
y2 = [6, 12, 8, 7, 14, 18, 20, 11]
y3 = [15, 10, 11, 8, 5, 12, 20, 23]
plt.plot(x,y1, marker = 'o', color='#f70d1a')
plt.plot(x,y2, marker = 'x', color='#08088A')
plt.plot(x,y3, marker = '+', color='#FFBF00')
plt.show()
Related Articles
Simple Matplotlib Animation ExampleMatplotlib Pie Chart
Python Line Plot Using Matplotlib
Python Matplotlib 3D Plot
How to save figure in matplotlib pyplot
Python Matplotlib Scatter Plot
Python Pandas Plotting
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Body Detection OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples