python画图代码大全
时间: 2023-09-05 17:10:01 浏览: 116
以下是常用的 Python 画图代码示例:
1. 散点图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x,y)
plt.show()
```
2. 折线图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y)
plt.show()
```
3. 条形图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = np.random.rand(5)
plt.bar(x,y)
plt.show()
```
4. 直方图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
plt.hist(x,bins=30)
plt.show()
```
5. 饼图
```python
import matplotlib.pyplot as plt
import numpy as np
sizes = [15, 30, 45, 10]
labels = ['A', 'B', 'C', 'D']
explode = (0, 0.1, 0, 0)
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.axis('equal')
plt.show()
```
6. 热力图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(100,100)
plt.imshow(x, cmap='hot', interpolation='nearest')
plt.show()
```
7. 3D图
```python
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.linspace(-10,10,100)
y = np.linspace(-10,10,100)
X,Y = np.meshgrid(x,y)
Z = np.sin(np.sqrt(X**2+Y**2))
surf = ax.plot_surface(X,Y,Z,cmap='coolwarm')
plt.show()
```
这些是常用的 Python 画图代码示例,你可以根据需要进行修改和优化。同时,也可以通过查阅 Matplotlib 官方文档来学习更多的画图技巧。
阅读全文