Python contour不等间距填色
时间: 2024-01-15 22:19:11 浏览: 325
以下是两种Python绘制不等间距填色图的例子:
1. 使用`contourf`函数绘制填色图,并设置不等间距的色条刻度:
```python
import matplotlib.pyplot as plt
# 定义经度、纬度和高度数据
lon = [1, 2, 3, 4, 5]
lat = [1, 2, 3, 4, 5]
z = [[-100, -90, -80, -70, -60],
[-50, -40, -30, -20, -10],
[-5, -4, -3, -2, -1],
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]
# 定义不等间距的色条刻度
level = [-100, -90, -80, -70, -60, -50, -40, -30, -20, -10, -1, 0]
# 绘制填色图
a = plt.contourf(lon, lat, z, level, extend='both', cmap='viridis')
# 设置色条的刻度
cb = plt.colorbar(a, shrink=0.78)
cb.set_ticks(level)
cb.ax.set_title('(m)', fontsize=12)
# 显示图形
plt.show()
```
2. 使用`contourf`函数绘制填色图,并设置不等间距的色条刻度:
```python
import matplotlib.pyplot as plt
# 定义经度、纬度和高度数据
lon = [1, 2, 3, 4, 5]
lat = [1, 2, 3, 4, 5]
z = [[-100, -90, -80, -70, -60],
[-50, -40, -30, -20, -10],
[-5, -4, -3, -2, -1],
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]
# 定义不等间距的色条刻度
level = range(-100, 1)
# 绘制填色图
a = plt.contourf(lon, lat, z, level, extend='both', cmap='viridis')
# 设置色条的刻度
cb = plt.colorbar(a, shrink=0.78)
cb.set_ticks(level)
cb.ax.set_title('(m)', fontsize=12)
# 显示图形
plt.show()
```
阅读全文