How to Plot List of Lists in Python: Various Methods

Suppose you have a list of lists and want to plot a chart for it. Then how you can do so? There are different methods to plot the list data. In this tutorial, you will know all the methods to plot a list of lists in python.

Methods to Plot List of Lists in Python

In this section, you will know all the methods to plot a list of lists in python.

Method 1: Plot using matplotlib module

Matplotlib is a library for Python that allows the user to generate various kinds of plots such as line charts, scatter plots, bar graphs, and more. To plot a list of lists, You must first convert them into a numpy array using the numpy.array() method. After that, the plot() function from matplotlib.pyplot can be used to visualize the data.

Use the below lines of code to plot the chart.

import matplotlib.pyplot as plt
import numpy as np
sample_list = [[10,20,30], [40,50,60], [70,80,90]]
data = np.array(sample_list)
plt.plot(sample_list)
plt.show()

Output

plotting list of list using the matplotlib module
plotting list of list using the matplotlib module

Method 2: Using the Seaborn module

Seaborn is a Python library that enables users to create beautiful and informative statistical graphics through the use of its high-level interface. The lineplot() function is also available which allows you to plot a list of lists as an example.

import seaborn as sns
sample_list = [[10,20,30], [40,50,60], [70,80,90]]
sns.lineplot(data=sample_list)
plt.show()

Output

plotting list of list using the seaborn module
plotting list of list using the seaborn module

You can see both of these libraries will plot your list of lists as a line plot. You can also use other plot types like scatter plots, bar plots, histograms, etc depending on the type of data you are using.

Method 3: Plot List of Lists in Python using the Plotly module

Plotly is a Python-based open-source library for data visualizations that comes with interactive and dynamic features. Using the plotly.graph_objs module and the plotly.offline.plot() function, anyone can create line plots, scatter plots, bar plots, and more from a list of lists.

It outputs the plot as an HTML file. Just open that file you will see plot.  An example of this shown below.

import plotly.graph_objs as go
import plotly.offline as offline
sample_list = [[10,20,30], [40,50,60], [70,80,90]]
trace = go.Scatter(y=sample_list)
offline.plot([trace])

Output

plotting list of list using the plotly module
plotting list of list using the plotly module

Method 4: Using the Bokeh module

Using the Bokeh library, you can create interactive visuals for modern web browsers with Python. Bokeh offers a user-friendly way to plot data sets like line plots or scatter plots. To plot a list of lists, first import the bokeh.plotting module and then use the line() function. An example is provided below:

from bokeh.plotting import figure, showsample_list = [[10,20,30], [40,50,60], [70,80,90]]
p = figure(title='line plot')
p.line(sample_list)
show(p)

Conclusion

There are several ways to plot a list of lists in Python. In this tutorial, you learned to plot a list of lists using different libraries like matplotlib,seaborn,plotly, etc. Depending on your project’s specific needs and requirements, you can use any method.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link