6.5. Line Chart

6.5.1. Simple line

import matplotlib.pyplot as plt


x = [1, 2, 3]
y = [4, 7, 5]

plt.plot(x, y)
plt.show()
../../_images/simple-line-1.png

6.5.2. Name axis and chart

import matplotlib.pyplot as plt


x = [1, 2, 3]
y = [4, 7, 5]

plt.plot(x, y)

plt.xlabel('X numbers')
plt.ylabel('Y numbers')
plt.title('Line Chart')

plt.show()
../../_images/simple-line-2.png

6.5.3. Subtitles

import matplotlib.pyplot as plt


x = [1, 2, 3]
y = [4, 7, 5]

plt.plot(x, y)

plt.xlabel('X numbers')
plt.ylabel('Y numbers')
plt.title('Line Chart\nThis will be the subtitle')

plt.show()
../../_images/simple-line-3.png

6.5.4. Legend

import matplotlib.pyplot as plt


x1 = [1, 2, 3]
y1 = [4, 7, 5]

x2 = [1, 2, 3]
y2 = [10, 14, 12]

plt.plot(x1, y1, label='First line', color='c')  # c - cyan
plt.plot(x2, y2, label='Second line', color='red')

plt.xlabel('X numbers')
plt.ylabel('Y numbers')
plt.title('Line Chart')
plt.legend()

plt.show()
../../_images/simple-line-4.png

6.5.5. Examples

6.5.5.1. Exponential functions

x = np.linspace(0, 2, 100)

plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')

plt.title('Exponential functions')
plt.xlabel('x')
plt.ylabel('y')

plt.legend()
plt.show()
../../_images/matplotlib-exponentials.png

6.5.5.2. Sin wave

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 0.2)
y = np.sin(x)

plt.plot(x, y)
plt.show()
../../_images/matplotlib-sin-wave.png

Figure 6.6. Sin wave