AITC Wiki

0402 Simple Scatter Plots

简单散点图

0402 Simple Scatter Plots

中文版:简单散点图

Simple Scatter Plots

Another commonly usedplottypeisthe simple scatter plot, a close cousin ofthelineplot. Instead of points being joined by line segments, here the points are represented individually withadot, circle, or other shape. We’ll start by setting up the notebook for plotting and importing the functions wewilluse:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

Scatter Plots with plt.plot

In the previous section we looked at plt.plot/ax.plot to produce line plots. It turns outthatthissame function can produce scatter plots as well:

x = np.linspace(0, 10, 30)
y = np.sin(x)
 
plt.plot(x, y, 'o', color='black');

The third argument in the function callisa character that represents thetypeof symbol usedforthe plotting. Justasyoucan specify options such as '-', '--' to control thelinestyle, the marker style hasitsownsetofshort string codes. Thefulllistof available symbols canbeseeninthe documentation of plt.plot, or in Matplotlib’s online documentation. Mostofthe possibilities are fairly intuitive, and we’llshowa number ofthemore common ones here:

rng = np.random.RandomState(0)
for marker in ['o', '.', ',', 'x', '+', 'v', '^', '<', '>', 's', 'd']:
 plt.plot(rng.rand(5), rng.rand(5), marker,
 label="marker='{0}'".format(marker))
plt.legend(numpoints=1)
plt.xlim(0, 1.8);

Forevenmore possibilities, these character codes canbeused together withlineandcolor codes to plot points along withaline connecting them:

plt.plot(x, y, '-ok');