6.11. Line Chart

6.11.1. Rationale

  • Show linear relation of two variables

6.11.2. Syntax

import matplotlib.pyplot as plt


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

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

6.11.3. Line Styles

../../_images/matplotlib-linestyle.png

6.11.4. Single Plot

Listing 6.77. 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()
Listing 6.78. 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()

6.11.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()
Listing 6.79. 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()
Listing 6.80. 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()
Listing 6.81. 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()
Listing 6.82. 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()