前提
matplotlibのimshowを使用して,海と陸を含んだ標高データのグラフを描きたいと考えています.
グラフの描画自体はmatplotlibの公式ドキュメントを参考に描くことができましたが,
カラーバーの調整に苦労しています.現在は以下のような画像の状況になっています.
上の画像を見ると,陸のカラーバーは正しく表示されていますが,同じ長さで,海のカラーバー(水深500m)が
表現されてしまっています.この海の部分を陸域の縮尺と一致(つまり,海のカラーバーを短くしたい)させたいと考えています.
回答よろしくお願いいたします.
実現したいこと
- 海のカラーバーの縮尺を陸のものと一致させたい
該当のソースコード
Python
1import numpy as np 2import matplotlib.pyplot as plt 3import matplotlib.colors as colors 4import matplotlib.cbook as cbook 5from matplotlib import cm 6 7dem = cbook.get_sample_data('topobathy.npz', np_load=True) 8topo = dem['topo'] 9longitude = dem['longitude'] 10latitude = dem['latitude'] 11 12fig, ax = plt.subplots() 13# make a colormap that has land and ocean clearly delineated and of the 14# same length (256 + 256) 15colors_undersea = plt.cm.terrain(np.linspace(0, 0.17, 256)) 16colors_land = plt.cm.terrain(np.linspace(0.25, 1, 256)) 17all_colors = np.vstack((colors_undersea, colors_land)) 18terrain_map = colors.LinearSegmentedColormap.from_list( 19 'terrain_map', all_colors) 20 21# make the norm: Note the center is offset so that the land has more 22# dynamic range: 23divnorm = colors.TwoSlopeNorm(vmin=-500., vcenter=0, vmax=4000) 24 25pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm, 26 cmap=terrain_map, shading='auto') 27# Simple geographic plot, set aspect ratio beecause distance between lines of 28# longitude depends on latitude. 29ax.set_aspect(1 / np.cos(np.deg2rad(49))) 30ax.set_title('TwoSlopeNorm(x)') 31cb = fig.colorbar(pcm, shrink=0.6) 32cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000]) 33plt.show()
補足情報(FW/ツールのバージョンなど)
matplotlib:3.5.1
参考サイト(https://matplotlib.org/3.5.0/tutorials/colors/colormapnorms.html#sphx-glr-tutorials-colors-colormapnorms-py)
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/06/07 06:23