mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 11:04:21 +08:00
82 lines
2.1 KiB
Markdown
82 lines
2.1 KiB
Markdown
# Pyplot 比例尺(Scales)
|
|
|
|
在不同的比例上创建图。这里显示了线性,对数,对称对数和对数标度。有关更多示例,请参阅库的[“缩放”](https://matplotlib.org/gallery/index.html#scales-examples)部分。
|
|
|
|
```python
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
from matplotlib.ticker import NullFormatter # useful for `logit` scale
|
|
|
|
# Fixing random state for reproducibility
|
|
np.random.seed(19680801)
|
|
|
|
# make up some data in the interval ]0, 1[
|
|
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
|
|
y = y[(y > 0) & (y < 1)]
|
|
y.sort()
|
|
x = np.arange(len(y))
|
|
|
|
# plot with various axes scales
|
|
plt.figure(1)
|
|
|
|
# linear
|
|
plt.subplot(221)
|
|
plt.plot(x, y)
|
|
plt.yscale('linear')
|
|
plt.title('linear')
|
|
plt.grid(True)
|
|
|
|
|
|
# log
|
|
plt.subplot(222)
|
|
plt.plot(x, y)
|
|
plt.yscale('log')
|
|
plt.title('log')
|
|
plt.grid(True)
|
|
|
|
|
|
# symmetric log
|
|
plt.subplot(223)
|
|
plt.plot(x, y - y.mean())
|
|
plt.yscale('symlog', linthreshy=0.01)
|
|
plt.title('symlog')
|
|
plt.grid(True)
|
|
|
|
# logit
|
|
plt.subplot(224)
|
|
plt.plot(x, y)
|
|
plt.yscale('logit')
|
|
plt.title('logit')
|
|
plt.grid(True)
|
|
# Format the minor tick labels of the y-axis into empty strings with
|
|
# `NullFormatter`, to avoid cumbering the axis with too many labels.
|
|
plt.gca().yaxis.set_minor_formatter(NullFormatter())
|
|
# Adjust the subplot layout, because the logit one may take more space
|
|
# than usual, due to y-tick labels like "1 - 10^{-3}"
|
|
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
|
|
wspace=0.35)
|
|
|
|
plt.show()
|
|
```
|
|
|
|

|
|
|
|
## 参考
|
|
|
|
此示例中显示了以下函数,方法,类和模块的使用:
|
|
|
|
```python
|
|
import matplotlib
|
|
matplotlib.pyplot.subplot
|
|
matplotlib.pyplot.subplots_adjust
|
|
matplotlib.pyplot.gca
|
|
matplotlib.pyplot.yscale
|
|
matplotlib.ticker.NullFormatter
|
|
matplotlib.axis.Axis.set_minor_formatter
|
|
```
|
|
|
|
## 下载这个示例
|
|
|
|
- [下载python源码: pyplot_scales.py](https://matplotlib.org/_downloads/pyplot_scales.py)
|
|
- [下载Jupyter notebook: pyplot_scales.ipynb](https://matplotlib.org/_downloads/pyplot_scales.ipynb) |