[Go to site: main page, start]

Line Chart

logo of a chart:Linelogo of a chart:Line

A line chart displays the evolution of one or several numeric variables. It is often used to represend time series.

The line chart is one of the most common chart type. As a result, all the most common python data visualization libraries like matplotlib, seaborn or plotly allow to build it.

This page displays many line chart examples made with those tools. It goes from basic line chart tutorials to highly customized, polished examples 🔥.

⏱ Quick start

Making a simple line chart with matplotlib is pretty straightforward thanks to the plot() function.

If you provide only a series of values, it will consider that these values are ordered and will use values from 1 to n to create the X axis.🔥

For more control on the chart, see the dedicated section below.

# libraries
import matplotlib.pyplot as plt
import numpy as np

# create data
values=np.cumsum(np.random.randn(1000,1))

# use the plot function
plt.plot(values)

🔎 plot() function parameters→ see full doc

→ Description

The plot() function of matplotlib is a versatile plotting function that can create various types of plots, including line plots, scatter plots, and step plots. It's one of the most fundamental plotting functions in matplotlib.

→ Arguments

Description

Positions of the data points along the x-axis.

Possible values → array-like

The x and y arrays must have the same length and contain numerical values.

Code Example

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x=x, y=y)
plt.show()

Matplotlib logoLine chart with several groups (Matplotlib)

A line chart with multiple groups allows to show the evolution of several items on the same figure.

It is powerful but can quickly turn into a spaghetti chart: when too many lines are displayed they get cluttered and hard to read. Moreover, make sure to use inline labeling instead of a side legend that is very annoying to read.

The examples below explain how to build a line chart with multiple groups, and what are the alternatives to show your data a better way.

Seaborn logoLine chart with Seaborn

Seaborn is another very good alternative when it comes to create line charts in Python. It comes with a powerful lineplot() function that does most of the work for us.

Plotly logoInteractive line chart with plotly

If you are looking for an interactive version of a line chart with Python, plotly is definitely the library you need.

Its API is very straightforward to understand, and the output allows to zoom on the chart and have tooltip for markers:

Pandas logoLine chart with Pandas

Pandas offers a simple and efficient way to create line charts directly from DataFrames, eliminating the need for complex data manipulation.

Its integration with Matplotliballows for extensive customization, making it a versatile choice for quick data visualization tasks.

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    dataviz decision tree poster