Python Matplotlib Tutorial

Matplotlib Line Plot – Python Matplotlib Tutorial

Matplotlib Line Plot

In this blog, you will learn how to draw a matplotlib line plot with different style and format.

The pyplot.plot() or plt.plot() is a method of matplotlib pyplot module use to plot the line.

Syntax: plt.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

Import pyplot module from matplotlib python library using import keyword and give short name plt using as  keyword.

import matplotlib.pyplot as plt 

Import Dataset of 15 days Delhi temperature record. The dataset in the form of list data type, you can use NumPy array, tuple, etc.

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]

Plot the line using plt.plot() method and show it using plt.show() method. Here, give a parameter x as a days and y as a temperature to plt.plot() 

plt.plot(days, temperature)
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi Temperature graph
Fig 1.1 – 15 days temperature record of Delhi city

Fig 1.1 not showing any useful information, because it has no x-axis,  y-axis, and title. So for this, you can use the below methods.

Syntax: plt.xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)

Syntax: plt.ylabel(ylabel, fontdict=None, labelpad=None, **kwargs)

Syntax: plt.title(label, fontdict=None, loc=‘center’, pad=None, **kwargs)

# plot line with x-axis, y-axis and title
plt.plot(days, temperature)
plt.title("Delhi Temperature")
plt.xlabel("days")
plt.ylabel("temperature")
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi Temperature graph with information
Fig 1.2 – 15 days temperature record of Delhi city with information

Observe Fig 1.1 and Fig 1.2, the starting axis value take automatically by plt.plot() method. If you want to set it manually, then use plt.axis() method.

Syntax: plt.axis(xmin, xmax, ymin, ymax)

plt.plot(days, temperature)
plt.axis([0,30, 0,50]) # set axis 
plt.title("Delhi Temperature")
plt.xlabel("days")
plt.ylabel("temperature")
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi Temperature graph set with axis
Fig 1.3 – 15 days temperature record of Delhi city set with the axis

The plt.plot() method has much more parameter. So, let’s play with some of them.

plt.plot(days, temperature, color = "g", marker = "o", linestyle= "--", linewidth = 3,
        markersize = 10)
plt.title("Delhi Temperature")
plt.xlabel("days")
plt.ylabel("temperature")
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi Temperature graph set with axis
Fig 1.4 – 15 days temperature record of Delhi city with parameters

Color Parameter Values

Character Color
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white

Marker Parameter Values

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
s square marker
p pentagon marker
* star marker
h hexagon1 marker
H hexagon2 marker
+ plus marker
x x marker
D diamond marker
d thin_diamond marker
| vline marker
_ hline marker

Line Style parameters values

Character Description
_ solid line style
dashed line style
_. dash-dot line style
: dotted line style

So, try to use different values of the above parameters. Then you will get a different output.

If you want to change the bar chart’s background color and add grid then use style.use() method. For this first, need to import the style module from matplotlib. 

Syntax: style.use(style)

To add a legend in the graph to describe more information about it, use plt.legend().

Syntax: plt.legend(*args, **kwargs)

from matplotlib import style # import style module
style.use("ggplot") # give ggplot parameter value to use() method
plt.plot(days, temperature, "mo--", linewidth = 3,
        markersize = 10, label = "Temp line")
plt.title("Delhi Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4) 
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi Temperature graph with style and legend
Fig 1.5 – 15 days temperature record of Delhi city with style and legend

Matplotlib Multiple Lines Plot

To plot multiple lines using a matplotlib line plot method use more plt.plot() method similar to your dataset.

Here, we have 15 days temperature record of Delhi and Mumbai city

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
delhi_tem = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
mumbai_tem = [39,39.4,40,40.7,41,42.5,43.5,44,44.9,44,45,45.1,46,47,46]

plt.plot(days, delhi_tem, "mo--", linewidth = 3,
        markersize = 10, label = "Delhi tem")

plt.plot(days, mumbai_tem, "yo:", linewidth = 3,
        markersize = 10, label = "Mumbai tem}")

plt.title("Delhi  & Mumbai Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4)
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi & Mumbai city Temperature graph
Fig 1.6 – 15 days temperature record of Delhi & Mumbai city

If you want to change or add grid then use plt.grid() method.

Syntax: plt.grid(b=None, which=‘major’, axis=‘both’, **kwargs)

plt.plot(days, delhi_tem, "mo-", linewidth = 3,
        markersize = 10, label = "Delhi tem")

plt.plot(days, mumbai_tem, "k-", linewidth = 5,
        markersize = 10, label = "Mumbai tem")

plt.title("Delhi  & Mumbai Temperature", fontsize=15)
plt.xlabel("days",fontsize=13)
plt.ylabel("temperature",fontsize=13)
plt.legend(loc = 4)
plt.grid(color='c', linestyle='-', linewidth=2) # grid with parameter
plt.show()

Output >>>

Matplotlib line plot - 15 days Delhi & Mumbai city Temperature graph with grid
Fig 1.7 – 15 days temperature record of Delhi & Mumbai city with a grid

In this way, you can plot multiple lines using matplotlib line plot method. 

Note: When you use style.use(“ggplot”). after that, no need to it again because it uses once and applies for all graph.

Conclusion

In matplotlib line plot blog, we learn how to plot one and multiple lines with a real-time example using plt.plot() method. Along with that used different method with different parameter. Suggest you make your hand dirty with each and every parameter of the above methods. This is the best coding practice. After completion of the matplotlib tutorial jump on Seaborn.

Download Jupyter file matplotlib line plot source code

Visite to the official site of matplotlib.org

Leave a Reply