利用matplotlib制作二维像素图

s5a0g9ez  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(147)

我有以下数据:

Latitude, Longitude, U component of wind

其中,纬度和经度是坐标,风的U分量是风速,单位为m/s。每个(纬度、经度、风的U分量)对/像素覆盖地面上(绘图上)的50000米(50 km)。有364个点(对),矩形区域的其余部分可以有白色。
我想用matplotlib准备一个2D图,每个像素根据第3列的值获得一种颜色(彩虹颜色从红色到紫色,从风列U分量的最小值到最大值),数据从CSV文件中读取。我想知道matplotlib的最佳方法是什么?
编辑CSV文件:CSV File
Image from CSV

vaqhlq81

vaqhlq811#

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection

# this part is about faking data
x = y =np.linspace(0, 1200, 24, endpoint=0)
X, Y = np.meshgrid(x, y)
positions = np.arange(24*24, dtype=int)

np.random.seed(2013-3-9)
np.random.shuffle(positions)
points = [(x, y) for x, y in zip(
    X.flatten()[positions[:364]], Y.flatten()[positions[:364]])]
velocities = np.random.rand(364)+(X+Y).flatten()[positions[:364]]/1000

with open('wind.csv', 'w') as out:
    for p, v in zip(points, velocities):
        print(*p, v, sep=',', file=out)

# start reading here
# read from CSV
with open('wind.csv') as inp:
    points, velocities = [], []
    for line in inp:
        x, y, v = (float(value) for value in line.split(','))
        points.append((x, y)), velocities.append(v)
 
pc = PatchCollection([plt.Rectangle(point, 50, 50) for point in points],
                     norm=plt.Normalize(), cmap='rainbow_r')
pc.set_array(velocities)

fig, ax = plt.subplots(figsize=(7,6), layout='constrained')
ax.set_aspect(1)
rectangles = ax.add_collection(pc)
ax.autoscale()
cb = fig.colorbar(rectangles)
cb.set_label('Normalized Wind Velocity', size='large')
plt.show()

相关问题