Line Chart


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

# 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)

Matplotlib logoLine chart with Matplotlib

Matplotlib is a great fit to build line charts thanks to its plot() function.

The first chart of this section explains how to use plot() from any kind of data input format. The next one goes deep into chart customization (line width, color aspect and more).

Another common need is to build a dual Y axis line chart, but be mindful of the caveats that go with it.

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.

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.

Matplotlib logoBest python line chart examples

The web is full of astonishing charts made by awesome bloggers, (often using R). The Python graph gallery tries to display (or translate from R) some of the best creations and explain how their source code works. If you want to display your work here, please drop me a word or even better, submit a Pull Request!

Contact


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥