前提
matplotlibのimshowを使用して,海と陸を含んだ標高データのグラフを描きたいと考えています.
グラフの描画自体はmatplotlibの公式ドキュメントを参考に描くことができましたが,
カラーバーの調整に苦労しています.現在は以下のような画像の状況になっています.
上の画像を見ると,陸のカラーバーは正しく表示されていますが,同じ長さで,海のカラーバー(水深500m)が
表現されてしまっています.この海の部分を陸域の縮尺と一致(つまり,海のカラーバーを短くしたい)させたいと考えています.
回答よろしくお願いいたします.
実現したいこと
- 海のカラーバーの縮尺を陸のものと一致させたい
該当のソースコード
Python
import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors import matplotlib.cbook as cbook from matplotlib import cm dem = cbook.get_sample_data('topobathy.npz', np_load=True) topo = dem['topo'] longitude = dem['longitude'] latitude = dem['latitude'] fig, ax = plt.subplots() # make a colormap that has land and ocean clearly delineated and of the # same length (256 + 256) colors_undersea = plt.cm.terrain(np.linspace(0, 0.17, 256)) colors_land = plt.cm.terrain(np.linspace(0.25, 1, 256)) all_colors = np.vstack((colors_undersea, colors_land)) terrain_map = colors.LinearSegmentedColormap.from_list( 'terrain_map', all_colors) # make the norm: Note the center is offset so that the land has more # dynamic range: divnorm = colors.TwoSlopeNorm(vmin=-500., vcenter=0, vmax=4000) pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm, cmap=terrain_map, shading='auto') # Simple geographic plot, set aspect ratio beecause distance between lines of # longitude depends on latitude. ax.set_aspect(1 / np.cos(np.deg2rad(49))) ax.set_title('TwoSlopeNorm(x)') cb = fig.colorbar(pcm, shrink=0.6) cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000]) plt.show()
補足情報(FW/ツールのバージョンなど)
matplotlib:3.5.1
参考サイト(https://matplotlib.org/3.5.0/tutorials/colors/colormapnorms.html#sphx-glr-tutorials-colors-colormapnorms-py)
まだ回答がついていません
会員登録して回答してみよう