Creating a 2D Line Chart in Python – Matplotlib Tutorial and Example

Matplotlib is a powerful python library for creating a 2D line chart, which is very useful to data analysis. We will show how to create this kind of chart with some steps for matplotlib beginners in this article.

Step-by-step Guide to Create a 2D Line Chart in Python - Matplotlib for Beginners

Download Python source code python-matplot-2d-line-chart.py  

How to create a 2d line chart using matplotlib?

1.Import some python libraries

import numpy as np
import matplotlib.pyplot as plt

2.Prepare sine(x) data

x = np.linspace(0, 2 * np.pi, 20)
y = np.sin(x)

where x will be displayed on x axis, y will be displayed on y axis

3.Create a 2d line chart

plt.plot(x, y, c='#ff6600')
 
plt.title('sin(x)')
plt.xlabel('x axis')
plt.ylabel('y axis')

where parameter c will set the line color.

4.Show 2d line chart

plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *