# python matplotlib库绘图实战之绘制散点图

## 一.导入库

`import matplotlib.pyplot as plt`

## 二.设置文字

```plt.title("double number", fontsize=24)
plt.xlabel("number", fontsize=14)
plt.ylabel("double", fontsize=14)
```

## 三.设置坐标轴参数

`plt.axis([0, 15, 0, 30])`

q前面两个数为x轴的始末，后面则为y

## 四.绘制点

```plt.scatter(2, 4, s=20) #s为点的大小
plt.show()
```

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

## 五.对点的继续处理

### 1.自定义颜色

```plt.scatter(x, y, c='red', edgecolors='none', s=20)
```

ps：将c设置为green，edgecolors设置为black，将引起极度生理不适，特别是点极度密集的曲线中，你懂的

```plt.scatter(x, y, c=(0, 0, 0.6), edgecolors='none', s=20)
```

RGB小数模式详见补充3

### 2.颜色映射

```plt.scatter(x, y, c=y, edgecolors='none', cmap=plt.cm.Blues, s=20)
```

one of {‘tab:blue’, ‘tab:orange’, ‘tab:green’, ‘tab:red’, ‘tab:purple’, ‘tab:brown’, ‘tab:pink’, ‘tab:gray’, ‘tab:olive’, ‘tab:cyan’} which are the Tableau Colors from the ‘tab10’ categorical palette (which is the default color cycle);

That’s all，接下来为补充部分

## 补充1

```x = list(range(1, 100, 2))
```

```y = [value * 2 for value in x]
```

## 补充2

Matplotlib识别以下格式以指定颜色：

an RGB or RGBA tuple of float values in [0, 1] (e.g. (0.1, 0.2, 0.5)
or (0.1, 0.2, 0.5, 0.3)). RGBA is short for Red, Green, Blue, Alpha; a
hex RGB or RGBA string (e.g., ‘#0F0F0F’ or ‘#0F0F0F0F’);

a string representation of a float value in [0, 1] inclusive for gray
level (e.g., ‘0.5’);

a X11/CSS4 (“html”) color name, e.g. “blue”; a name from the xkcd
color survey, prefixed with ‘xkcd:’ (e.g., ‘xkcd:sky blue’); a “Cn”
color spec, i.e. ‘C’ followed by a number, which is an index into the
default property cycle (rcParams[“axes.prop_cycle”] (default:
cycler(‘color’, [’#1f77b4’, ‘#ff7f0e’, ‘#2ca02c’, ‘#d62728’,
‘#9467bd’, ‘#8c564b’, ‘#e377c2’, ‘#7f7f7f’, ‘#bcbd22’, ‘#17becf’])));
the indexing is intended to occur at rendering time, and defaults to
black if the cycle does not include color. one of {‘tab:blue’,
‘tab:orange’, ‘tab:green’, ‘tab:red’, ‘tab:purple’, ‘tab:brown’,
‘tab:pink’, ‘tab:gray’, ‘tab:olive’, ‘tab:cyan’} which are the Tableau
Colors from the ‘tab10’ categorical palette (which is the default
color cycle);

## 补充3

RGB分为浮点数和整数两种，在Matplotlib中，使用的是浮点数，即范围在[0,1],而整数则是[0,255],若是要转化，将整数除以255即浮点数形式。