In this article, we will learn how to customize a line graph in Jupyter Notebook, we will discover different customization options available to customize the line graph we will use matplotlib library to generate and customize line graphs we will understand each option by looking it into the syntax along with the examples, Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is a plotting library for the Python programming language and its numerical mathematics extension NumPy.
Jupyter Notebook
The Jupyter Notebook is the original web application for creating and sharing computational documents that contain live code, equations, visualizations, and narrative text. It offers a simple, streamlined, document-centric experience. Jupyter has support for over 40 different programming languages and Python is one of them.
Line Graph
Line graph also known as a line chart or line plot is a type of data visualization, line graphs are useful for displaying data that shows trends, changes over time, or relationships between variables.
Importance of customizing graphs
- Pattern Recognition: Visual representation of data helps to identify the patterns and trends in the data
- Data Analysis: Visual representation of data allows users to filter information, and gain deeper insights.
- Decision Making: Improves the decision-making of an organization by identifying the opportunities and potential risks in a pictorial view.
- Memory Retention: People tend to remember more graphical information rather than numerical or text.
- Efficiency: Allows users to quickly identify key insights, trends, and outliers and saves alot of time.
Customize Line Graph
To understand the line graph first we plot a simple line graph with very less customization, to plot the line graph use plot() method of matplotlib module,
- import matplotlib.pyplot module
- Define the values for x-axis and y-axis
- Plot the line graph using plot(x,y) method
- To show the graph use show() method
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y) plt.show() |
Output:
Customizing labels, title and legend
To add title to the line graph use the title method of matplotlib module,
plt.title(‘Your Title of Line Graph’)
To add label to x-axis,
plt.xlabel(‘Label of x-axis’)
To add label to y-axis,
plt.ylabel(‘Label of y-axis’)
To add label to line plotted in the graph,
plt.plot(x,y,label=’Label for your line’)
plt.legend()
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.legend() |
Output:
Customizing color of the graph
To customize the color on the line graph, use the option color available in the plot() method,
plt.plot(x,y,color=’your color’)
Note: you can use colors defined by matplotlib module or you can use the hex code to define color e.g. #008000
Some commonly used colors:
Character |
Color |
---|---|
b |
blue |
g |
green |
r |
red |
c |
cyan |
m |
magenta |
y |
yellow |
k |
black |
w |
white |
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ,color = 'm' ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.legend() plt.show() |
Output:
Customizing Markers in line graph
To customize marker in the line graph, simply use the marker option available in the plot() method, additional options for markers:
Option |
Description |
---|---|
markersize |
Set the marker size in points |
markerfacecolor |
Set the marker face color |
markeredgecolor |
Set the marker edge color |
markeredgewidth |
Set the marker edge width in points |
Markers available:
Character |
Description |
---|---|
. |
point marker |
, |
pixel marker |
o |
circle marker |
v |
triangle_down marker |
^ |
triangle_up marker |
< |
triangle_left marker |
> |
triangle_right marker |
1 |
tri_down marker |
2 |
tri_up marker |
3 |
tri_left marker |
4 |
tri_right marker |
8 |
octagon marker |
s |
square marker |
p |
pentagon marker |
P |
plus (filled) marker |
* |
star marker |
h |
hexagon1 marker |
H |
hexagon2 marker |
+ |
plus marker |
x |
x marker |
X |
x (filled) marker |
D |
diamond marker |
d |
thin_diamond marker |
| |
vline marker |
_ |
hline marker |
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ,color = 'm' , marker = '*' , markersize = 20 , markeredgecolor = 'black' , markerfacecolor = 'red' ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.legend() plt.show() |
Output:
Customizing Line Styles and line width in line graph
To customize line styles there is a option available in the plot() method and by setting it you can customize the linestyle in graph and to set the width use linewidth option to define the width.
Line Style
Character |
Description |
---|---|
‘-‘ or ‘solid’ |
solid line style |
‘–‘ or ‘dashed’ |
dashed line style |
‘-.’ or ‘dashdot’ |
dash-dot line style |
‘:’ or ‘dotted’ |
dotted line style |
‘ ‘ or ‘None’ |
no line style |
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ,color = 'm' , marker = '*' , markersize = 15 , linestyle = 'None' , linewidth = 2 ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.legend() plt.show() |
Output:
Add Gridlines in Line Graph
To add griplines in the graph there is a method matplotlib.pyplot.grid() we add gridlines we just have to pass True in the method
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ,color = 'm' , marker = '*' , markersize = 20 , markeredgecolor = 'b' , markerfacecolor = 'g' ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.grid( True ) plt.legend() plt.show() |
Output:
Adding Axis limit to line graph
By adding axis limit we are limiting the axis and focusing on a particular region, we use matplotlib.pyplot.xlim(start,end) and matplotlib.pyplot.ylim(start,end) to limit the x-axis and y-axis repectively and start and end both are included.
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ,color = 'm' , marker = '*' , markersize = 20 , markeredgecolor = 'b' , markerfacecolor = 'g' ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.grid( True ) plt.legend() plt.xlim( 0 , 50 ) plt.ylim( 0 , 100 ) plt.show() |
Output:
Annotations in Line Graph
To highlight specific data or add and text in the grahp we use annotations, to add annotations we use matplotlib.pyplot.annotate(text, xy, xytext=None, xycoords=’data’, textcoords=None, arrowprops=None, annotation_clip=None, **kwargs) method.
Option |
Description |
---|---|
text : str |
The text of the annotation. |
xy : (float, float) |
The point (x, y) to annotate. The coordinate system is determined by xycoords. |
xytext : (float, float), default: xy |
The position (x, y) to place the text at. The coordinate system is determined by textcoords |
xycoords : single or two-tuple of str or Artist or Transform or callable, default: ‘data’t |
The coordinate system that xy is given in |
textcoords : single or two-tuple of str or Artist or Transform or callable, default: value of xycoords |
The coordinate system that xytext is given in. |
arrowprops : dict, optional |
The properties used to draw a FancyArrowPatch arrow between the positions xy and xytext. Defaults to None |
annotation_clipbool : bool or None, default: None |
Whether to clip (i.e. not draw) the annotation when the annotation point xy is outside the axes area. |
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] plt.plot(x,y,label = 'TEST' ,color = 'm' , marker = '*' , markersize = 20 , markeredgecolor = 'b' , markerfacecolor = 'g' ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.legend() plt.annotate( 'Annotation' , xy = ( 30 , 23 ), xytext = ( 2 , - 0.8 ), arrowprops = dict (arrowstyle = '->' , connectionstyle = 'arc3,rad=-0.5' ), fontsize = 16 , color = 'green' ) plt.show() |
Output:
Multiple Lines in the graph
To add multiple lines to the graph simply put one more matplotlib.pyplot.plot() method along with the attributes to add one more line,
Example:
Python3
import matplotlib.pyplot as plt x = [ 10 , 20 , 30 , 40 , 50 ] y = [ 12 , 34 , 23 , 43 , 30 ] x1 = [ 5 , 15 , 25 , 35 , 45 ] y1 = [ 20 , 40 , 10 , 20 , 40 ] plt.plot(x,y,label = 'A' ,color = 'm' , marker = '*' , markersize = 20 ) plt.plot(x1,y1,label = 'B' ,color = 'g' , marker = 'h' , markersize = 10 ) plt.xlabel( 'x-axis' ) plt.ylabel( 'y-axis' ) plt.title( 'Customizing Line Chart' ) plt.legend() plt.show() |
Output:
Conclusion
In this article we discussed on line graph its features how to customize line graphs different options available to customize the line graph like line style, marker, color, labels, title etc along with examples to each customization.