> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist-python/visualization/pyplot.md).

# pyplot

Provides a MATLAB-like plotting framework. `pylab`combines pyplot with numpy into a single namespace.

## Visualization basics

We create X axis that start in 0 and goes to 5, with 0.1 steps. Then we create Y axis using SIN function where we pass X as input.

```
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
```

## Here is the output. ![](https://463299088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3wXpaYFUfPiqG2WRb2%2F-M3wY0wnXPvGBdZ6_Cbt%2F-M3wY6LAsxHmcNFjFWHr%2FScreen%20Shot%202017-05-19%20at%2010.33.16%20AM.png?generation=1585858846268513\&alt=media)Scatter plot

Lets create a simple scatter plot to introduce basics of scatter plot.

```
import matplotlib.pyplot as plt

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

colors = (0, 0, 0)
plt.scatter(x, y, c=colors, alpha=0.5)

plt.title('Scatter plot')
plt.xlabel('x')
plt.ylabel('y')

plt.show()
```

![](https://463299088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3wXpaYFUfPiqG2WRb2%2F-M3wY0wnXPvGBdZ6_Cbt%2F-M3wY6LCtNZPG-Zk-63L%2FScreen%20Shot%202017-05-19%20at%2012.14.35%20PM.png?generation=1585858845499487\&alt=media)When we want to visualize different groups of points, we might want to color the points.

```
import numpy as np
import matplotlib.pyplot as plt

N = 10
g1 = (0.6 + 0.6 * np.random.rand(N), np.random.rand(N))
g2 = (0.4 + 0.3 * np.random.rand(N), 0.5 * np.random.rand(N))
g3 = (0.3 * np.random.rand(N), 0.3 * np.random.rand(N))

data = (g1, g2, g3)
colors = ("red", "green", "blue")
groups = ("coffee", "tea", "water")

# Create plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, axisbg="1.0")

for data, color, group in zip(data, colors, groups):
    x, y = data
    ax.scatter(x, y, alpha=0.8, c=color, edgecolors='none', s=30, label=group)

plt.title('scatter plot')
plt.legend(loc=2)
plt.show()
```

![](https://463299088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3wXpaYFUfPiqG2WRb2%2F-M3wY0wnXPvGBdZ6_Cbt%2F-M3wY6LEApi6mDgWuCVO%2FScreen%20Shot%202017-05-19%20at%2012.17.19%20PM.png?generation=1585858847671814\&alt=media)
