matplotlib库的各种图形pyth
时间: 2023-07-12 10:36:21 浏览: 88
Python使用Matplotlib绘制多种图形
on代码示例
1. 折线图(line plot):
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
```
2. 散点图(scatter plot):
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(size=100)
y = np.random.normal(size=100)
plt.scatter(x, y)
plt.show()
```
3. 条形图(bar plot):
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.array(['A', 'B', 'C', 'D'])
y = np.array([3, 7, 2, 5])
plt.bar(x, y)
plt.show()
```
4. 直方图(histogram):
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(size=100)
plt.hist(x, bins=20)
plt.show()
```
5. 饼图(pie chart):
```python
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
sizes = [20, 30, 40, 10]
plt.pie(sizes, labels=labels)
plt.show()
```
6. 箱线图(box plot):
```python
import matplotlib.pyplot as plt
import numpy as np
data = np.random.normal(size=(100, 5))
plt.boxplot(data)
plt.show()
```
7. 热力图(heatmap):
```python
import matplotlib.pyplot as plt
import numpy as np
data = np.random.normal(size=(10, 10))
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.show()
```
以上是一些常见的 Matplotlib 图形的 Python 代码示例,可以通过修改数据和参数来进一步探索和定制这些图形。
阅读全文