Understanding Matplotlib Line Plot

Matplotlib Line Plot provides functionalities to create high-quality line plots to represent data in a clear and concise manner.

In this article, we will take a closer look at how to generate line plots with examples and different customization options.



Matplotlib Line Plot

To create a Matplotlib line plot, we first need to import the library and define the data points that we want to plot.

Here is an example:

Example: 

import matplotlib.pyplot as plt# Define data points x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]# Create line plot plt.plot(x, y)# Show the plot plt.show()

In above example, we defined two lists, x and y, that represent the x and y coordinates of our data points. We then created a line plot using the plt.plot() function, passing in the x and y lists as arguments.

Finally, we used the plt.show() function to display the plot.


Customizing Matplotlib Line Plots

Matplotlib provides various customization options to make our line plots more informative and visually appealing.

Here are some common customization options that we can use:

Linestyle

The default line style is a solid line, but we can also use dashed, dotted, or dash-dot lines.

You can customize the style of the plotted line through the keyword argument linestyle, or simply ls:

In the following example utilize linestyle = ‘dotted’ argument:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, linestyle = 'dotted') pt.show()

Implement the dotted line style with a pentagon marker:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'p', linestyle = 'dotted') pt.show()

In the below example apply linestyle = ‘dashed‘ argument:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, linestyle = 'dashed') pt.show()

Utilize the dashed line style with a hexagon marker:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'H', linestyle = 'dashed') pt.show()

Shorter Syntax

There is a shorter syntax for the line style:

  • Linestyle is represented by ls.
  • dotted is represented as :.
  • Dashed is represented as .

Implement the simple version of the line style argument:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, ls = '–') pt.show()

First apply the marker argument then set the line style to ‘dotted‘:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'H', ls = ':') pt.show()

These are the styles you can pick from:

StylesOr
‘solid’ (default)‘-‘
‘dotted’‘:’
‘dashed’‘–‘
‘dashdot’‘-.’
‘None’” or ‘ ‘

Line Color

If you want to modify the color of the line, you can pass the keyword argument color or the smaller version c:

Make the line color black in the below example:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, ls = '–', color = 'k') pt.show()

Utilize the simpler version of color c:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'P', ls = ':', c = 'm') pt.show()

You can also generate your own color with a hexadecimal code:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, ls = '–', color = '#e6005c') pt.show()

Display the following graph with ‘#003d4d‘ color:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'P', ls = ':', c = '#003d4d') pt.show()

Alternatively, you can select any of the 140 available color names.

Show the graph below with tomato color:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, ls = '–', color = 'Tomato') pt.show()

Insert the ‘yellowgreen‘ color in the c argument:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'P', ls = ':', c = 'YellowGreen') pt.show()

Line Width

To modify the width of the line, you can choose from the keyword argument linewidth or the simpler lw argument.

The value of linewidth is represent in a points form.

Increase the weight of the following graph line:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, ls = '–', color = 'Tomato', linewidth = '15.5') pt.show()

Apply the simple version of the linewidth argument with the ‘10.5‘ value:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane,marker = 'P', ls = ':', c = 'YellowGreen', lw = '10.5') pt.show()

Multiple Lines

If you provide multiple plt.plot() functions, then you will be able to generate the number of lines you want.

To sketch each line, utilize the plt.plot() function:

Example: 

import matplotlib.pyplot as pt import numpy as npy y1_plane = npy.array([20, 31, 64, 10, 50]) y2_plane = npy.array([49, 37, 77, 74, 9]) pt.plot(y1_plane, ls = '–', color = '#e6005c') pt.plot(y2_plane, ls = '–', color = '#cc0000') pt.show()

Implement the plt.plot() function two times for two lines:

Example: 

import matplotlib.pyplot as pt import numpy as npy y1_plane = npy.array([67, 44, 67, 22]) y2_plane = npy.array([39, 80, 47, 80]) pt.plot(y1_plane,marker = 'H', ls = ':', c = '#004d00', lw = '5.5') pt.plot(y2_plane,marker = '>', ls = '-.', c = '#ff4000', lw = '5.5') pt.show()

Through the plt.plot() function, you can display many lines by including x- and y-axis points for each line.

As you can see in the example above, we only defined the points on the y-axis, which means that the points on the x-axis got their default values (0, 1, 2, 3) as well.

It is important to note that the x- and y-values appear in pairs.

Provide the x1, y1, x2 and y2 point values for both lines and plot two lines:

Example: 

import matplotlib.pyplot as pt import numpy as npy x1_plane = npy.array([20, 31, 64, 10, 50]) x2_plane = npy.array([49, 37, 77, 74, 9]) y1_plane = npy.array([32, 84, 27, 45, 6]) y2_plane = npy.array([41, 74, 97, 55, 37]) pt.plot(x1_plane, x2_plane, y1_plane, y2_plane, ls = '–', color = 'Aquamarine') pt.show()

Display the two lines in the following example with x and y points values:

Example: 

import matplotlib.pyplot as pt import numpy as npy x1_plane = npy.array([32, 84, 27, 45, 6]) x2_plane = npy.array([41, 74, 97, 55, 37]) y2_plane = npy.array([29, 37, 43, 101, 59]) y1_plane = npy.array([63, 31, 33, 71, 2]) pt.plot(x1_plane, x2_plane, y1_plane, y2_plane, ls = ':', lw = '5.5', color = 'Black') pt.show()

Example Explanation

The above example utilizes the Matplotlib library to plot a graph.

  • It defines four arrays, x1_plane, x2_plane, y1_plane, and y2_plane, which represent the x and y coordinates for two planes.
  • The data in these arrays is then used to plot a graph using the “plot” function from Matplotlib.
  • The graph is plotted with dotted lines (“ls = ‘:'”), thick line width (“lw = ‘5.5’”), and black color (“color = ‘Black'”).

Finally, the “show” function is used to display the graph.


Matplotlib Line Plots Benefits

  • Matplotlib line plots are straightforward to create with only a few lines of code, making them ideal for data analysts and scientists who require quick visualization of their data.
  • They are particularly helpful for displaying trends over time, enabling easy comparison of data changes and identification of patterns and outliers.
  • Matplotlib line plots are highly customizable, enabling users to adjust the color, line style, width, and marker style of the lines to create visually appealing and effective plots that communicate the intended message.
  • Matplotlib line plots can handle massive datasets with ease, allowing users to plot thousands of data points without performance loss.
  • Matplotlib integrates well with other Python libraries such as NumPy and Pandas, making it easy to create line plots from data stored in these libraries.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *