Matplotlib is a data visualization library in Python. Thepyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line chartsare used to represent the relation between two data X and Y on a different axis. In this article, we will learn about line charts and matplotlib simple line plots inPython. Here, we will see some of the examples of a line chart in Python usingMatplotlib:
Matplotlib Simple Line Plot
Example 1:In this example, a simple line chart is generated usingNumPyto define data values. The x-values are evenly spaced points, and the y-values are calculated as twice the corresponding x-values.
Pythonimportmatplotlib.pyplotaspltimportnumpyasnpx=np.array([1,2,3,4])# X-axisy=x*2# Y-axisplt.plot(x,y)plt.show()
Output
Simple line plot between X and Y dataExplanation: This is a basic line chart wherex contains four points andy is calculated as twice of eachx value. plt.plot() creates the line and plt.show()renders the plot.
Example 2:We can see in the above output image that there is no label on the x-axis and y-axis. Since labeling is necessary for understanding the chart dimensions. In the following example, we will see how to add labels, Ident in the charts.
Pythonimportmatplotlib.pyplotaspltimportnumpyasnpx=np.array([1,2,3,4])y=x*2plt.plot(x,y)plt.xlabel("X-axis")# Label for the X-axisplt.ylabel("Y-axis")# Label for the Y-axisplt.title("Any suitable title")# Chart titleplt.show()
Output
Simple line plot with labels and titleLine Chart with Annotations
In this example, a line chart is created using sample data points. Annotations displaying the x and y coordinates are added to each data point on the line chart for enhanced clarity.
Pythonimportmatplotlib.pyplotaspltx=[1,2,3,4,5]y=[2,4,6,8,10]plt.figure(figsize=(8,6))plt.plot(x,y,marker='o',linestyle='-')# Add annotationsfori,(xi,yi)inenumerate(zip(x,y)):plt.annotate(f'({xi},{yi})',(xi,yi),textcoords="offset points",xytext=(0,10),ha='center')plt.title('Line Chart with Annotations')plt.xlabel('X-axis Label')plt.ylabel('Y-axis Label')plt.grid(True)plt.show()
Output

Explanation:data points are marked with circles (marker='o') and labeled usingplt.annotate()to show their exact coordinates.
Multiple Line Charts Using Matplotlib
We can display more than one chart in the same container by usingpyplot.figure() function. This will help us in comparing the different charts and also control the look and feel of charts.
Pythonimportmatplotlib.pyplotaspltimportnumpyasnpx=np.array([1,2,3,4])y=x*2plt.plot(x,y)plt.xlabel("X-axis")plt.ylabel("Y-axis")plt.title("Any suitable title")plt.show()# show first chartplt.figure()x1=[2,4,6,8]y1=[3,5,7,9]plt.plot(x1,y1,'-.')plt.show()
Output


Explanation:Here, two different line charts are plotted sequentially in separate figures usingplt.figure().The first plot uses solid lines (default), and the second uses a dashed-dot pattern ('-.').
Multiple Plots on the Same Axis
Here, we will see how to add 2 plots within the same axis.
Pythonimportmatplotlib.pyplotaspltimportnumpyasnpx=np.array([1,2,3,4])y=x*2# first plot with X and Y dataplt.plot(x,y)x1=[2,4,6,8]y1=[3,5,7,9]# second plot with x1 and y1 dataplt.plot(x1,y1,'-.')plt.xlabel("X-axis data")plt.ylabel("Y-axis data")plt.title('multiple plots')plt.show()
Output

Explanation: This plots two lines in the same figure and on the same axes. It allows direct comparison of different datasets. The second line uses a different style for visual distinction.
Fill the Area Between Two Lines
Using thepyplot.fill_between() function we can fill in the region between two line plots in the same graph. This will help us in understanding the margin of data between two line plots based on certain conditions.
Pythonimportmatplotlib.pyplotaspltimportnumpyasnpx=np.array([1,2,3,4])y=x*2plt.plot(x,y)x1=[2,4,6,8]y1=[3,5,7,9]plt.plot(x,y1,'-.')plt.xlabel("X-axis data")plt.ylabel("Y-axis data")plt.title('multiple plots')plt.fill_between(x,y,y1,color='green',alpha=0.5)plt.show()
Output
Fill the area between Y and Y1 data corresponding to X-axis dataExplanation:Highlights the area between two line plots usingplt.fill_between().The alpha parameter adjusts transparency.
Plot a Line Chart in Python using Matplotlib