python logo

Matplotlib Line chart


Python hosting: Host, run, and code Python in the cloud!

Matplotlib is a popular Python library that allows users to create a variety of visualizations, including line charts. This guide provides step-by-step instructions on how to create a line chart using Matplotlib. Let’s get started!

Creating a line chart in Matplotlib is straightforward with the plot() function. This guide offers a comprehensive tutorial on the various customization and enhancements available in Matplotlib for line charts.

Matplotlib offers versatility beyond just plotting a basic line. It provides options to define the grid, x and y axis scales, labels, titles, and many other display features.

Related course:

Basic Line Chart Example

Consider the example below which illustrates the process of creating a basic line chart.

1
2
3
4
5
6
7
8
9
10
from pylab import *
t = arange(0.0, 2.0, 0.01)
s = sin(2.5*pi*t)
plot(t, s)

xlabel('time (s)')
ylabel('voltage (mV)')
title('Sine Wave')
grid(True)
show()

Python Matplotlib Line Chart

The above code is initialized by defining the data to be plotted. The plot(t, s) function is then called to render the chart. The additional commands, like xlabel(), ylabel(), and title(), help set the x-axis text, y-axis text, and the chart title, respectively. The grid(True) command activates the grid display.

To save the plot to disk, use the savefig("line_chart.png") command.

Custom Line Chart

Matplotlib also supports plotting using lists or arrays. Here’s how you can plot a line chart using a list:

1
2
3
4
5
6
7
8
9
10
11
from pylab import *

t = arange(0.0, 20.0, 1)
s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
plot(t, s)

xlabel('Item (s)')
ylabel('Value')
title('Python Line Chart: Plotting numbers')
grid(True)
show()

Python Line Chart from List

Multiple Plots

To create a chart with multiple lines, you simply need to call the plot() function multiple times, as demonstrated below:

1
2
3
4
5
6
7
8
9
10
11
12
13
from pylab import *

t = arange(0.0, 20.0, 1)
s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
s2 = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
plot(t, s)
plot(t, s2)

xlabel('Item (s)')
ylabel('Value')
title('Python Line Chart: Plotting Multiple Series')
grid(True)
show()

Python Line Chart Multiple Series

For displaying multiple plots in different sections of the same window, consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import matplotlib.pyplot as plt
from pylab import *

t = arange(0.0, 20.0, 1)
s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
s2 = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]

plt.subplot(2, 1, 1)
plt.plot(t, s)
plt.ylabel('Value')
plt.title('First Series')
plt.grid(True)

plt.subplot(2, 1, 2)
plt.plot(t, s2)
plt.xlabel('Item (s)')
plt.ylabel('Value')
plt.title('Second Series')
plt.grid(True)
plt.show()

Python Subplots

The function plt.subplot() is crucial in this context. It allows you to specify the number of rows, columns, and the figure number.

Styling the Plot

For a customized appearance of the line chart, like setting a specific color or line thickness, the following command can be used:

1
plot(t, s, color="red", linewidth=2.5, linestyle="-")

For further insights and sample codes, you can download the Matplotlib examples here.

Previous Tutorial | Next Tutorial






Leave a Reply: