写出Matplotlib包中pyplot模块绘制散点图、线性图、柱状图、直方图、饼图、箱线图的定义语句。
时间: 2023-10-20 22:10:46 浏览: 112
利用Python – Matplotlib 绘制箱线图
散点图:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)
plt.show()
```
线性图:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
```
柱状图:
```python
import matplotlib.pyplot as plt
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 8, 5, 12, 7]
plt.bar(x, y)
plt.show()
```
直方图:
```python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
x = np.random.normal(0, 1, 1000)
plt.hist(x, bins=30)
plt.show()
```
饼图:
```python
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
sizes = [40, 30, 20, 10]
plt.pie(sizes, labels=labels)
plt.show()
```
箱线图:
```python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
data = np.random.normal(0, 1, 100)
plt.boxplot(data)
plt.show()
```
阅读全文