4.1. Line Chart

4.1.1. Rationale

  • Show linear relation of two variables

4.1.2. Syntax

import matplotlib.pyplot as plt


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

plt.plot(x, y)
plt.show()

4.1.3. Line Styles

../_images/matplotlib-plt-linestyle-basic.png
../_images/matplotlib-plt-linestyle-advanced.png

4.1.4. Single Plot

Code 4.151. Vectorized Operations
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)

x = np.arange(0, 10)
y = np.random.randint(0, 10, size=10)

plt.plot(x, y)
plt.show()
Code 4.152. Universal Function
import matplotlib.pyplot as plt
import numpy as np


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

plt.plot(x, y)
plt.show()

4.1.5. Multiple Plots

import matplotlib.pyplot as plt


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

x2 = [1, 2, 3, 4]
y2 = [4, 3, 3, 2]

plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()
Code 4.153. Universal Function
import matplotlib.pyplot as plt
import numpy as np


x = np.linspace(0, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1)
plt.plot(x, y2)
plt.show()
Code 4.154. Inlined Universal Function
import matplotlib.pyplot as plt
import numpy as np


x = np.linspace(0, 10, 1000)

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
Code 4.155. Vectorized Operation
import matplotlib.pyplot as plt
import numpy as np


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

plt.plot(x, x)
plt.plot(x, x**2)
plt.plot(x, x**3)
plt.show()
Code 4.156. Universal Function and Vectorized Operation
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)


noise = np.random.normal(0.0, 0.1, size=1000)

x1 = np.linspace(0, 2*np.pi, 1000)
y1 = np.sin(x1) + noise

x2 = np.linspace(2*np.pi, 3*np.pi, 20)
y2 = np.sin(x2)

plt.plot(x1, y1)
plt.plot(x2, y2, linestyle='--')
plt.show()