代码之家  ›  专栏  ›  技术社区  ›  s28

用于轮廓图的Python绘图颜色栏

  •  1
  • s28  · 技术社区  · 2 年前

    我有以下代码来生成一个绘图:

    import numpy as np
    import matplotlib.pyplot as plt
    import xarray as xr
    
    data = xr.open_dataset('mypath/data.nc')  
    
    lat=np.array(data['latitude'][:])
    lon=np.array(data['longitude'][:])
    u = np.array(data['u'][:])
    u = u.reshape(len(lat),len(lon))
    
    fig, ax = plt.subplots(1,1,figsize=(8, 6))
    X,Y=np.meshgrid(lon,lat)
    ax.contourf(X,Y,u)
    

    Python Plot

    如何添加颜色条?

    我试过plt.colorbar()。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Mahendran R    2 年前

    要在绘图中添加颜色条,可以指定的结果 ax.contourf 调用一个变量,然后将该变量传递给 plt.colorbar() 。以下是修改代码的方法:

    fig, ax = plt.subplots(1, 1, figsize=(8, 6))
    X, Y = np.meshgrid(lon, lat)
    contour = ax.contourf(X, Y, u)
    plt.colorbar(contour)
    
    plt.show()